版本比较
题解:P2346 四子连棋
正文
增加 70 行删除 66 行
31 行未改动323334353636373840414243444546474849505152533940414243444546474849505152535456555658595758596162636465666768697071727374757677787980818283848586878889606162636465666768697071727374757677787980818283848586878889909192939495929697949596979899100989910010110210310410510610310410510610710810911011111211310710810911011111211311411511611711831 行未改动#include <bits/stdc++.h>using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗const int n = 4;char s[5][5];const int D[4] = {-4 , 4 , -1 , 1}; bool check(string &b){ for(int i = 0 ; i < 4 ; ++i){ int p = i * 4; if(b[p] != 'O' && b[p] == b[p + 1] && b[p + 1] == b[p + 2] && b[p + 2] == b[p + 3]) return 1; } for(int j = 0 ; j < 4 ; ++j){ if(b[j] != 'O' && b[j] == b[j + 4] && b[j + 4] == b[j + 8] && b[j + 8] == b[j + 12]) return 1; } if(b[0] != 'O' && b[0] == b[5] && b[5] == b[10] && b[10] == b[15]) return 1; if(b[3] != 'O' && b[3] == b[6] && b[6] == b[9] && b[9] == b[12]) return 1; return 0; for(int i = 0 ; i < 4 ; ++i){ int p = i * 4; if(b[p] != 'O' && b[p] == b[p + 1] && b[p + 1] == b[p + 2] && b[p + 2] == b[p + 3]) return 1; } for(int j = 0 ; j < 4 ; ++j){ if(b[j] != 'O' && b[j] == b[j + 4] && b[j + 4] == b[j + 8] && b[j + 8] == b[j + 12]) return 1; } if(b[0] != 'O' && b[0] == b[5] && b[5] == b[10] && b[10] == b[15]) return 1; if(b[3] != 'O' && b[3] == b[6] && b[6] == b[9] && b[9] == b[12]) return 1; return 0;}int h(string &b){ return check(b) ? 0 : 1; return check(b) ? 0 : 1;}bool dfs(string cur , int turn , int g , int limit , unordered_set<string> &vis){ if(g + h(cur) > limit)bool dfs(string cur , int turn , int g , int lit , unordered_set<string> &vis){ if(g + h(cur) > lit) return 0; if(check(cur)) return 1; int p1 = -1 , p2 = -1; for(int i = 0 ; i < 16 ; ++i) if(cur[i] == 'O'){ if(p1 == -1) p1 = i; else p2 = i; } int blank[2] = {p1 , p2}; char cur_c = (turn == 0 ? 'B' : 'W'); for(int b = 0 ; b < 2 ; ++b){ int pos = blank[b]; int x = pos / 4 , y = pos % 4; for(int k = 0 ; k < 4 ; ++k){ if(k == 2 && y == 0) continue; if(k == 3 && y == 3) continue; int npos = pos + D[k]; if(npos < 0 || npos >= 16) continue; if(cur[npos] != cur_c) continue; string nxt = cur; swap(nxt[pos] , nxt[npos]); if(vis.count(nxt)) continue; vis.insert(nxt); if(dfs(nxt , 1 - turn , g + 1 , limit , vis)) return 1; vis.erase(nxt); } } return 0; if(check(cur)) return 1; int p1 = -1 , p2 = -1; for(int i = 0 ; i < 16 ; ++i) if(cur[i] == 'O'){ if(p1 == -1) p1 = i; else p2 = i; } int blank[2] = {p1 , p2}; char cur_c = (turn == 0 ? 'B' : 'W'); for(int b = 0 ; b < 2 ; ++b){ int pos = blank[b]; int x = pos / 4 , y = pos % 4; for(int k = 0 ; k < 4 ; ++k){ if(k == 2 && y == 0) continue; if(k == 3 && y == 3) continue; int npos = pos + D[k]; if(npos < 0 || npos >= 16) continue; if(cur[npos] != cur_c) continue; string nxt = cur; swap(nxt[pos] , nxt[npos]); if(vis.count(nxt)) continue; vis.insert(nxt); if(dfs(nxt , 1 - turn , g + 1 , lit , vis)) return 1; vis.erase(nxt); } } return 0;}int IDA(string start , int turn){ if(check(start)) if(check(start)) return 0; for(int i = 1 ; i <= 50 ; ++i){ unordered_set<string> vis; vis.insert(start); if(dfs(start , turn , 0 , i , vis)) return i; } return 100; for(int i = 1 ; i <= 50 ; ++i){ unordered_set<string> vis; vis.insert(start); if(dfs(start , turn , 0 , i , vis)) return i; } return -1;}int main(){ //freopen(".in" , "r" , stdin); //freopen(".out" , "w" , stdout); for(int i = 0 ; i < 4 ; ++i) scanf("%s" , s[i]); string start = ""; for(int i = 0 ; i < 4 ; ++i) for(int j = 0 ; j < 4 ; ++j) start += s[i][j]; int ans = min(IDA(start , 0) , IDA(start , 1)); printf("%d" , ans); return 0; //freopen(".in" , "r" , stdin); //freopen(".out" , "w" , stdout); for(int i = 0 ; i < 4 ; ++i) scanf("%s" , s[i]); string start = ""; for(int i = 0 ; i < 4 ; ++i) for(int j = 0 ; j < 4 ; ++j) start += s[i][j]; int ans = min(IDA(start , 0) , IDA(start , 1)); printf("%d" , ans); return 0;}