正文
增加 12 行删除 12 行
30 行未改动3132333435363738394021 行未改动626364656667686970717273747576777820 行未改动991001011021031041051061073 行未改动1111121131141151161171181192 行未改动1221231241251261271281291309 行未改动1401411421431441451461471483 行未改动15215315415515615715815916016116216316416514 行未改动30 行未改动// 因为有三钟颜色,所以使用三进制编码(确保状态唯一)int encode(char sm[4][5]) {int ret = 0;inc(i, 0, n - 1)inc(j, 0, m - 1)ret = ret * 3 + sm[i][j];return ret;}21 行未改动else p = 2;}// 正向颜色变换:0->2, 1->0, 2->1int change[3] = {2, 0, 1};// 反向颜色变换:0->1, 1->2, 2->0int restore[3] = {1, 2, 0};//change 与 restore 操作互逆int main() {freopen("colball.in", "r", stdin);freopen("colball.out", "w", stdout);while (scanf("%d", &n) == 1 && n) {//结束为 0 所以要判断一下scanf("%d", &m);char sm[4][5], tm[4][5], backup[4][5]; // backup用于完整状态备份20 行未改动memset(vvv, -1, sizeof(vvv));memset(table, 0, sizeof(table));while (!que[0].empty()) que[0].pop();//防止数据污染 全部出队列while (!que[1].empty()) que[1].pop();que[0].push(start_enc);que[1].push(target_enc);//进队列insert(start_enc, 0, 0);insert(target_enc, 1, 0);//两个方向 步数为03 行未改动while (!que[0].empty() && !que[1].empty() && !found) { //如果队列不为空(没搜完) 与 还没有搜到答案int p = (que[0].size() <= que[1].size()) ? 0 : 1; //优化1:平衡策略,选择探索范围小的进行拓展,防止陷入 dfs 的僵局int sz = que[p].size();while (sz-- && !found) { //如果队列不为空(没搜完) 与 当前轮次还没有搜到答案int cur = que[p].front(); que[p].pop();//取出队头int scur = table[cur];decode(cur, sm);//编码2 行未改动// 遍历所有2x2区域inc(i, 1, n-1) {inc(j, 1, m-1) {memcpy(sm, backup, sizeof(backup)); // 确保每次从原始状态开始// ========= 操作1:旋转 =========int a = sm[i-1][j-1];if (p == 0) { // 正向:顺时针sm[i-1][j-1] = sm[i][j-1];9 行未改动// 计算新状态int nxt = encode(sm);int qv = vvv[nxt];if (qv == -1) { // 新状态insert(nxt, p, scur + 1);que[p].push(nxt);} else if (qv != p) { // 双向相遇3 行未改动}// ========= 操作2:颜色变换 =========memcpy(sm, backup, sizeof(backup)); // 恢复原始状态inc(x, i - 1, i) inc(y, j - 1, j)sm[x][y] = (p == 0) ? change[sm[x][y]] : restore[sm[x][y]];nxt = encode(sm);qv = vvv[nxt];if (qv == -1) {insert(nxt, p, scur + 1);que[p].push(nxt);} else if (qv != p) {14 行未改动30 行未改动3132333435363738394021 行未改动626364656667686970717273747576777820 行未改动991001011021031041051061073 行未改动1111121131141151161171181192 行未改动1221231241251261271281291309 行未改动1401411421431441451461471483 行未改动15215315415515615715815916016116216316416514 行未改动30 行未改动// 因为有三钟颜色,所以使用三进制编码(确保状态唯一)int encode(char sm[4][5]) {int ret = 0;inc(i, 0, n - 1)inc(j, 0, m - 1)ret = ret * 3 + sm[i][j];return ret;}21 行未改动else p = 2;}// 正向颜色变换:0->2, 1->0, 2->1int change[3] = {2, 0, 1};// 反向颜色变换:0->1, 1->2, 2->0int restore[3] = {1, 2, 0};//change 与 restore 操作互逆int main() {freopen("colball.in", "r", stdin);freopen("colball.out", "w", stdout);while (scanf("%d", &n) == 1 && n) {//结束为 0 所以要判断一下scanf("%d", &m);char sm[4][5], tm[4][5], backup[4][5]; // backup用于完整状态备份20 行未改动memset(vvv, -1, sizeof(vvv));memset(table, 0, sizeof(table));while (!que[0].empty()) que[0].pop();//防止数据污染 全部出队列while (!que[1].empty()) que[1].pop();que[0].push(start_enc);que[1].push(target_enc);//进队列insert(start_enc, 0, 0);insert(target_enc, 1, 0);//两个方向 步数为03 行未改动while (!que[0].empty() && !que[1].empty() && !found) { //如果队列不为空(没搜完) 与 还没有搜到答案int p = (que[0].size() <= que[1].size()) ? 0 : 1; //优化1:平衡策略,选择探索范围小的进行拓展,防止陷入 dfs 的僵局int sz = que[p].size();while (sz-- && !found) { //如果队列不为空(没搜完) 与 当前轮次还没有搜到答案int cur = que[p].front(); que[p].pop();//取出队头int scur = table[cur];decode(cur, sm);//编码2 行未改动// 遍历所有2x2区域inc(i, 1, n-1) {inc(j, 1, m-1) {memcpy(sm, backup, sizeof(backup)); // 确保每次从原始状态开始// ========= 操作1:旋转 =========int a = sm[i-1][j-1];if (p == 0) { // 正向:顺时针sm[i-1][j-1] = sm[i][j-1];9 行未改动// 计算新状态int nxt = encode(sm);int qv = vvv[nxt];if (qv == -1) { // 新状态insert(nxt, p, scur + 1);que[p].push(nxt);} else if (qv != p) { // 双向相遇3 行未改动}// ========= 操作2:颜色变换 =========memcpy(sm, backup, sizeof(backup)); // 恢复原始状态inc(x, i - 1, i) inc(y, j - 1, j)sm[x][y] = (p == 0) ? change[sm[x][y]] : restore[sm[x][y]];nxt = encode(sm);qv = vvv[nxt];if (qv == -1) {insert(nxt, p, scur + 1);que[p].push(nxt);} else if (qv != p) {14 行未改动