This is a creation in Article, where the information may have evolved or changed.
Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5546
Test Instructions: There are two people playing chess in a 9*9 chessboard, A is x, B is O, "." The meaning is empty. Ask A to be able to kill B's pieces (similar to go).
How to solve the problem: DFS to determine whether the block around the only one '. ’.
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;char map[20][20];int dir[ 4][2]= {{1,0},{-1,0},{0,1},{0,-1}},visit[20][20],ans,ant;void dfs (int x,int y) {int tx,ty,i; Visit[x][y]=ans; for (i=0; i<4; i++) {tx=x+dir[i][0]; TY=Y+DIR[I][1]; if (map[tx][ty]== ' x ') continue; if (tx<0| | tx>=9| | ty<0| | ty>=9| | Visit[tx][ty]==ans) continue; if (map[tx][ty]== '. ') {ant++; Visit[tx][ty]=ans; } else {DFS (tx,ty); }}}int Fuck () {int i,j; ans=0; For (i=0, i<9; i++) {for (j=0; j<9; J + +) {if (map[i][j]== ' O ' &&!visit[i][j]) {ans++; ant=0; DFS (I,J); if (ant==1) return 1; }}} return 0;} int main () {int t,i,k; Freopen ("In.txt", "R", stdin); Freopen("OUT.txt", "w", stdout); scanf ("%d", &t); k=1; GetChar (); while (t--) {memset (visit,0,sizeof (visit)); for (i=0; i<9; i++) {scanf ("%s", Map[i]); } printf ("Case #%d:", k++); if (Fuck ()) printf ("Can kill in one move!!! \ n "); else printf ("Can not kill in one move!!! \ n "); } return 0;}
Feel a little hanging some DFS. (template) *-*.