题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546
题意就是两个人下围棋,问在下一颗x是否能杀死o,‘.‘是空位子;
枚举所有的点,判断是否合法即可;
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <map> #include <queue> #include <stack> #include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a)) #define N 103 #define INF 0x3f3f3f3f typedef long long LL; int dir[4][2] = { {1,0}, {-1,0}, {0,-1}, {0,1} }; char s[N][N]; int vis[N][N]; int OK(int x, int y) { return (x<9 && y<9 && x>=0 && y>=0); } int check(int x, int y) { vis[x][y] = 1; for(int i=0; i<4; i++) { int px = x+dir[i][0]; int py = y+dir[i][1]; if(!OK(px, py) || vis[px][py])continue; if(s[px][py] == ‘.‘) return 1; if(s[px][py] == ‘o‘ && check(px, py)) return 1; } return 0; } int Judge(int x, int y) { for(int i=0; i<4; i++) { int px = x + dir[i][0]; int py = y + dir[i][1]; if(!OK(px, py))continue; if(s[px][py] == ‘o‘) { met(vis, 0); if(!check(px, py)) return 1;///不能走出去说明找到了; } } return 0; } int solve() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(s[i][j] == ‘x‘)continue; s[i][j] = ‘x‘; if(Judge(i, j))return 1; s[i][j] = ‘.‘; } } return 0; } int main() { int T, t = 1; scanf("%d", &T); while(T--) { for(int i=0; i<9; i++) { scanf("%s", s[i]); } int ans = solve(); if(ans) printf("Case #%d: Can kill in one move!!!\n", t++); else printf("Case #%d: Can not kill in one move!!!\n", t++); } return 0; }
时间: 2024-10-27 07:54:46