广搜(棋)

题目网址: http://acm.uestc.edu.cn/#/problem/show/1221

Ancient Go

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

Submit Status

Yu Zhou likes to play Go with Su Lu. From the historical research, we found that there are much difference on the rules between ancient go and modern go.

Here is the rules for ancient go they were playing:

  • The game is played on a 8×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×9 different positions to put the chess.
  • Yu Zhou always takes the black and Su Lu the white. They put the chess onto the game board alternately.
  • The chess of the same color makes connected components(connected by the board lines), for each of the components, if it‘s not connected with any of the empty cells, this component dies and will be removed from the game board.
  • When one of the player makes his move, check the opponent‘s components first. After removing the dead opponent‘s components, check with the player‘s components and remove the dead components.

One day, Yu Zhou was playing ancient go with Su Lu at home. It‘s Yu Zhou‘s move now. But they had to go for an emergency military action. Little Qiao looked at the game board and would like to know whether Yu Zhou has a move to kill at least one of Su Lu‘s chess.

Input

The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Test cases are separated by an empty line. Each test case consist of 9 lines represent the game board. Each line consists of 9 characters. Each character represents a cell on the game board. . represents an empty cell. x represents a cell with black chess which owned by Yu Zhou. o represents a cell with white chess which owned by Su Lu.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is Can kill in one move!!! if Yu Zhou has a move to kill at least one of Su Lu‘s components. Can not kill in one move!!! otherwise.

Sample input and output

Sample Input Sample Output
2

.......xo
.........
.........
..x......
.xox....x
.o.o...xo
..o......
.....xxxo
....xooo.

......ox.
.......o.
...o.....
..o.o....
...o.....
.........
.......o.
...x.....
........o
Case #1: Can kill in one move!!!
Case #2: Can not kill in one move!!!

Hint

In the first test case, Yu Zhou has 4 different ways to kill Su Lu‘s component.

In the second test case, there is no way to kill Su Lu‘s component.

Source

The 2015 China Collegiate Programming Contest

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
///http://acm.hust.edu.cn/vjudge/contest/view.action?cid=100505#problem/D
using namespace std;
char map[15][15];
bool vis[15][15];
int qx[100],qy[100];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int s1,s2;
int sum=0;

int bfs(int i,int j)
{
    int flag;
    flag=1;
    sum=0;
    int l=0,r=0;
    qx[r]=i;
    qy[r++]=j;
    vis[i][j]=1;
    while(l<r)
    {
        int curx=qx[l],cury=qy[l++];
        for(int t=0; t<4; t++)
        {
            int nx=curx+dir[t][0];
            int ny=cury+dir[t][1];
            if(sum>=1&&nx>=0&&nx<=8&&ny>=0&&ny<=8&&!vis[nx][ny]&&map[nx][ny]==‘.‘)
            {
                flag=0;
                vis[s1][s2]=0;
            }
            if(nx>=0&&nx<=8&&ny>=0&&ny<=8&&!vis[nx][ny]&&map[nx][ny]==‘o‘)
            {
                vis[nx][ny]=1;
                qx[r]=nx;
                qy[r++]=ny;
            }
            if(nx>=0&&nx<=8&&ny>=0&&ny<=8&&!vis[nx][ny]&&map[nx][ny]==‘.‘)
            {
                if(flag)
                {
                    sum++;
                    s1=nx;
                    s2=ny;
                    vis[nx][ny]=1;
                }
            }
        }
    }
    if(flag)
    return 1;
    return 0;
}

int main()
{
    int T,ca=1;
    int i,j,f;
    cin>>T;
    while(T--)
    {
        f=0;
        for(i=0;i<9;i++)
        cin>>map[i];
        memset(vis,0,sizeof(vis));
        for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        {
            if(map[i][j]==‘o‘&&!vis[i][j])
            {
                f=bfs(i,j);
                if(f) goto endw;
            }
        }
        endw:;
        if(f)
            printf("Case #%d: Can kill in one move!!!\n",ca++);
        else
            printf("Case #%d: Can not kill in one move!!!\n",ca++);
    }
    return 0;
}
时间: 2024-08-10 15:11:44

广搜(棋)的相关文章

7.23 深搜广搜

深搜(DFS)  关键词:回溯 栈实现,(递归本质和栈一样)一直走到底再回溯,时间复杂度高,空间低 #include<iostream> #include<cstring> using namespace std; int R,C; char maps[40][40]; int dp[40][40]; int dir[2][4]={{1,0,0,-1},{0,1,-1,0}}; int ans=1<<30; void DFS(int x,int y,int step){

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

codevs 1225:八数码难题【双向广搜】

这里是传送门 这道题用普通BFS是可以做的,但是很明显没得过,效率太低了.效率更高的算法A*和双向广搜都可取,这写一下双向广搜的. 注意题目中的判重很重要,可以转化成九位数用hash来解决这个问题. #include <set> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

宽搜和广搜、

广搜与深搜的小区别 一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(好比3步就必需达到前提)"的标题,它的空间效率高,然则找到的不必定是最优解,必需记实并完成全数搜索,故一般情况下,深搜需要很是高效的剪枝(优化). 像搜索最短路径这些的很显著若是用广搜,因为广搜的特征就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的操作,像什么起码状态转换也是可以操作的.深搜就

hdu1241详解 Java广搜搞定

import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int m = sc.nextInt();//输入地图的行数 int n = sc.nextInt();//输入地图的列数 if (m == 0) {//若m=0则退出程序 break; } // 初始化图

poj 1166 The Clocks 记录路径的广搜

题意: 给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点). 分析: 明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点.2.queue<node>比queue<int>要多花1别的时间. //poj 1166 //sep9 #include <iostream> #include

nyoj 523 双向广搜

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstdio> #include<queue> using namespace std; /* 用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索 双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径. */ const int N

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform