做爲一個弱渣,比賽時各類花式wa,今天嘗試作了作,發現那時候理解的題意都不對。ios
題意:把一個'.'換成'x'是否能夠由'x'圍起來一些字母,並且這些字母全是'o'。(以前覺得能圍住'o'就行,原來題意是圍住的裏面也不能有'.')。spa
題意:枚舉每個'.',使其變成'x',在這一點周圍四個點爲方向進行DFS,遇到'x'返回,若是存在一個DFS過程當中沒發現'.'即存在。code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #include <cstdlib> #include <map> using namespace std; char str[15][15]; bool visit[15][15]; int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; int f; void DFS(int x, int y) { int xx, yy; if(str[x][y]=='.') { f=1; return ; } if(str[x][y]=='x') return ; for(int i=0; i<4; i++) { xx = x+dir[i][0]; yy = y+dir[i][1]; if(xx>=1&&xx<=9&&yy>=1&&yy<=9&&visit[xx][yy]==false) { visit[xx][yy] = true; DFS(xx, yy); } } } int solve() { for(int i=1; i<=9; i++) { for(int j=1; j<=9; j++) { if(str[i][j]=='.') { str[i][j] = 'x'; int xx, yy; for(int k=0; k<4; k++) { xx = i+dir[k][0]; yy = j+dir[k][1]; if(xx>=1&&xx<=9&&yy>=1&&yy<=9&&str[xx][yy]=='o') { memset(visit, false, sizeof(visit)); visit[xx][yy] = true; f = 0; DFS(xx, yy); if(f==0) return 1; } } str[i][j] = '.'; } } } return 0; } int main() { int t; scanf("%d", &t); for(int tt=1; tt<=t; tt++) { for(int i=1; i<=9; i++) { scanf("%s", str[i]+1); } memset(visit, false, sizeof(visit)); int ans = solve(); if(ans) printf("Case #%d: Can kill in one move!!!\n", tt); else printf("Case #%d: Can not kill in one move!!!\n", tt); } return 0; }