poj 1970(搜索)

The Game

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6247   Accepted: 1601

Description

A game of Renju is played on a 19*19 board by two players. One player uses black stones and the other uses white stones. The game begins in an empty board and two players alternate in placing black stones and white stones. Black always goes first. There are 19 horizontal lines and 19 vertical lines in the board and the stones are placed on the intersections of the lines.

Horizontal lines are marked 1, 2, ..., 19 from up to down and vertical lines are marked 1, 2, ..., 19 from left to right.

The objective of this game is to put five stones of the same color
consecutively along a horizontal, vertical, or diagonal line. So, black
wins in the above figure. But, a player does not win the game if more
than five stones of the same color were put consecutively.

Given a configuration of the game, write a program to determine
whether white has won or black has won or nobody has won yet. There will
be no input data where the black and the white both win at the same
time. Also there will be no input data where the white or the black wins
in more than one place.

Input

The
first line of the input contains a single integer t (1 <= t <=
11), the number of test cases, followed by the input data for each test
case. Each test case consists of 19 lines, each having 19 numbers. A
black stone is denoted by 1, a white stone is denoted by 2, and 0
denotes no stone.

Output

There
should be one or two line(s) per test case. In the first line of the
test case output, you should print 1 if black wins, 2 if white wins, and
0 if nobody wins yet. If black or white won, print in the second line
the horizontal line number and the vertical line number of the left-most
stone among the five consecutive stones. (Select the upper-most stone
if the five consecutive stones are located vertically.)

Sample Input

1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output

1
3 2
错了N次。。这个题坑点在于只能是五子棋,6子,7子都不行,所以对一个点的某一个方向来说正反都要搜一遍。而且还要注意是结果是要位于左上角的点。所以可以先将某一列的每一行先找一遍,这样的话得到的结果就一定是左上角的点。给大家两组测试用例:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 2 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0输出是0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 1 0 2 2 2 1 0 0 0 0 0 0 0 0 0 00 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 00 1 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 01 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 00 0 1 1 0 1 2 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0答案是16 1
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
int graph[20][20];
bool vis[20][20];
int cnt;
int res,resx,resy;
int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};
bool check(int x,int y,int flag)
{
    if(x<1||x>19||y<1||y>19||graph[x][y]!=flag) return false;
    return true;
}
struct Node
{
    int x,y;
    int step;
};
Node s;
bool bfs(int x,int y,int flag)
{
    Node now;
    now.x = x,now.y = y,now.step = 0;
    Node next;
    for(int i=0; i<8; i++)
    {
        next.x = now.x+dir[i][0];
        next.y = now.y +dir[i][1];
        next.step = now.step+1;
        while(check(next.x,next.y,flag))
        {
            next.x+=dir[i][0];
            next.y+=dir[i][1];
            next.step++;
        }
        int step1 = next.step - 1;
        next.x = now.x+dir[i^1][0]; ///反方向也要找
        next.y = now.y +dir[i^1][1];
        next.step = now.step+1;
        while(check(next.x,next.y,flag))
        {
            next.x+=dir[i^1][0];
            next.y+=dir[i^1][1];
            next.step++;
        }
        int step2 = next.step - 1;
        if(step1+step2==4) return true;
    }
    return false;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        for(int i=1; i<20; i++)
        {
            for(int j=1; j<20; j++)
            {
                scanf("%d",&graph[i][j]);
            }
        }
        bool flag = false;
        res = 0,resx=-1,resy=-1;
        for(int j=1; j<20&&!flag; j++)
        {
            for(int i=1; i<20&&!flag; i++)
            {
                if(graph[i][j]==1)
                {
                    flag =  bfs(i,j,1);
                    if(flag)
                    {
                        res = 1;
                        resx = i;
                        resy = j;
                    }
                }
                if(graph[i][j]==2)
                {
                    flag =  bfs(i,j,2);
                    if(flag)
                    {
                        res = 2;
                        resx = i;
                        resy = j;
                    }
                }
            }
        }
        if(res==0) printf("0\n");
        else printf("%d\n%d %d\n",res,resx,resy);
    }
    return 0;
}
时间: 2024-10-18 11:39:45

poj 1970(搜索)的相关文章

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

poj 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

poj 1129 搜索

Channel Allocation Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description When a radio station is broadcasting over a very large area, repeaters are used to ret

POJ基础搜索题练习(计划6-8题)

Problems POJ2531 - Network Saboteur POJ3009 - Curling 2.0 POJ3414 - Pots Solutions POJ2531 - Network Saboteur 题目大意:N个节点的完全图(2<=N<=20),以邻接矩阵C的形式给出节点两两之间连边的权值.现在需要将这些节点分为A.B两个子集,使得∑Cij (i∈A, j∈B)最大. 考虑到节点总数不超过20,所以直接枚举子集的复杂度O(2^N)是可以接受的,要更进一步节省时间的话,一来

poj 3074,搜索枝剪

Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8909   Accepted: 3196 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . . . 6 7 3 5 . . .

POJ 1970 The Game

The Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6886   Accepted: 1763 Description A game of Renju is played on a 19*19 board by two players. One player uses black stones and the other uses white stones. The game begins in an emp

poj 折半搜索

poj2549 Sumsets 题目链接: http://poj.org/problem?id=2549 题意:给你一个含有n(n<=1000)个数的数列,问这个数列中是否存在四个不同的数a,b,c,d,使a+b+c=d;若存在则输出最大的d 思路:完全暴力的话O(n^4),会T,可以考虑双向搜索,公式变形为a+b=d-c:分别枚举a+b和c-d,将值和下标存在结构体中,再二分查找即可 #include<cstdio> #include<iostream> #include&

X - The Game POJ - 1970

题目翻译: 一个由19位的19人组成的玩家,由两名玩家组成.一个玩家使用黑石头,另一个玩家使用白石.游戏开始于一个空白的棋盘,两个玩家交替放置黑石头和白石.黑色总是先走.板上有19条水平线和19条垂直线,石头放在线路的交点处. 水平线从上到下标记为1,2,...,19,垂直线从左到右标记为1,2,...,19. 这个游戏的目标是沿着水平,垂直或对角线连续地放置相同颜色的五个石头.所以黑色在上图中赢了.但是,如果超过五颗相同颜色的石头连续投放,玩家就不会赢得比赛. 给定一个游戏的配置,写一个程序,

Oil Deposits (poj 1241) 搜索 (深度优先搜素)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8274 Accepted Submission(s): 4860 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground