#include #include #include bool isEnd(std::vector > v) { if (v[3][3] != 0)return false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == 3 && j == 3) { return true; } if (v[i][j] != (j + 1) + i * 4) { return false; } } } return true; } int main() { std::vector > v(4); std::stack > s; for (int j = 0; j < 4; j++) { int num; for (int i = 0; i < 4; i++) { std::cin >> num; v[j].push_back(num); if (num == 0) { s.push(std::make_pair(i, j)); } } } if(isEnd(v)) { std::cout << "Yes" << std::endl; return 0; } bool move[16]; for (int i = 0; i < 16; i++) { move[i] = false; } move[0] = true; while (true) { int x = s.top().first; int y = s.top().second; s.pop(); int n = (x + 1) + y * 4; if (move[n]) break; if (x != 0 && v[y][x - 1] == n) { v[y][x] = n; s.push(std::make_pair(x - 1, y)); v[y][x - 1] = 0; move[n] = true; }else if(x != 3 && v[y][x + 1] == n) { v[y][x] = n; s.push(std::make_pair(x + 1, y)); v[y][x + 1] = 0; move[n] = true; } else if (y != 0 && v[y - 1][x] == n) { v[y][x] = n; s.push(std::make_pair(x, y - 1)); v[y - 1][x] = 0; move[n] = true; } else if (y != 3 && v[y + 1][x] == n) { v[y][x] = n; s.push(std::make_pair(x, y + 1)); v[y + 1][x] = 0; move[n] = true; } else { break; } } if (isEnd(v)) { std::cout << "Yes" << std::endl; } else { std::cout << "No" << std::endl; } return 0; }