提交时间:2024-01-23 16:33:16
运行 ID: 125199
#include <bits/stdc++.h> using namespace std; int n; int a[20][20]; int tab[9][9] = {{1, 1, 0, 1, 1, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 0, 1, 1, 0, 0, 0}, {1, 0, 0, 1, 0, 0, 1, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 1, 0}, {0, 0, 1, 0, 0, 1, 0, 0, 1}, {0, 0, 0, 1, 1, 0, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 0, 1, 1}}; queue<pair<string, string> > que; string ans; void rotate(int what, int dir){ for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { a[i][j] += tab[what][i*3+j]*dir; a[i][j] %= 4; } } } string tstring() { string s; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { s += a[i][j] + '0'; } } return s; } void bfs() { while (true) { string s = que.front().second; string last = que.front().first; que.pop(); bool flag = true; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { a[i][j] = s[i*3+j]-'0'; if (a[i][j] != 0) flag = false; } } if (flag) { ans = last; return; } for (int i=0; i<=9; i++) { rotate(i, 1); que.push({last+char(i+1+'0'), tstring()}); rotate(i, -1); rotate(i, -1); que.push({last+char(i+1+'0'), tstring()}); rotate(i, 1); } } } signed main() { string s; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { int n; cin >> n; s += char(n-'0'); } } que.push({"", s}); bfs(); printf(ans.c_str()); return 0; }