这图最多3色就可以 搜2就行了
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<vector> 6 #include<algorithm> 7 #include<string> 8 using namespace std; 9 int vis[55][55],c[55][55],f[10],mm; 10 int dis[6][2] = {{1,0},{0,1},{-1,0},{0,-1},{-1,1},{1,-1}},n; 11 int flag,m; 12 int judge(int x,int y) 13 { 14 if(x<0||y<0||x>=n||y>=n) 15 return 0; 16 return 1; 17 } 18 int dfs(int x,int y,vector <string> board,int o) 19 { 20 int i; 21 for(i = 0 ; i < 6 ; i++) 22 { 23 int tx = x+dis[i][0]; 24 int ty = y+dis[i][1]; 25 if(judge(tx,ty)&&board[tx][ty]=='X') 26 { 27 if(c[tx][ty]) 28 { 29 if(c[tx][ty]!=o) 30 return 0; 31 } 32 else 33 { 34 flag = 1; 35 c[tx][ty] = o; 36 if(!dfs(tx,ty,board,-o)) 37 return 0; 38 } 39 } 40 } 41 return 1; 42 } 43 class HexagonalBoard 44 { 45 public: 46 int minColors(vector <string> board) 47 { 48 int i,j,k = board.size(); 49 n = k; 50 int ff = 1; 51 for(i = 0 ; i < n ; i++) 52 { 53 for(j = 0 ; j < n ; j++) 54 { 55 if(board[i][j]=='X'&&!c[i][j]) 56 { 57 m++; 58 c[i][j] = 1; 59 if(!dfs(i,j,board,-1)) 60 { 61 ff=0; 62 break; 63 } 64 } 65 } 66 if(!ff) break; 67 } 68 if(!m) return 0; 69 if(!flag) 70 return 1; 71 if(ff) 72 return 2; 73 else 74 return 3; 75 } 76 };