哈理工OJ 1031 _OOOO_ Problem(简单模拟问题)

来源:互联网 发布:js年龄正则表达式 编辑:程序博客网 时间:2024/04/29 08:30

OOOO Problem
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 288(96 users) Total Accepted: 90(83 users) Rating: Special Judge: No
Description
Do you know Gobang?
There is a 15 * 15 board and two players drop piece of white or black one by one.
We define a pattern of “+OOOO+” as “Huo 4”.
‘O’ means the piece of you.
‘X’ means the piece of your opponent.
‘+’ means the blank space of the board.
How many “Huo 4” belonging to you in the board ?

Input
Line 1 : A integer T which means there are T test cases following seperated by a blank line.
For each test case, the input will show a 15 * 15 board.

Output
For each test case, output the answer in a line.

Sample Input
2

+++++++++++++++
++OOOO++++++O++
++++++++++++O++
++++++++++++O++
++O+++++++++O++
+++O+++++++++++
++++O++++++++++
+++++O++++++O++
+++++++++++O+++
++++++++++O++++
+++++++++O+++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++

++++++++++++X++
++OOOOX+++++O++
++++++++++++O++
++++++++++++O++
++O+++++++++O++
+++O+++++++++++
++++O++++++++X+
+++++O++++++O++
++++++X++++O+++
++++++++++O++++
+++++++++O+++++
+++++++++++++++
+++++++++++++++
+XXXX++++++++++
+++++++++++++++
Sample Output
4
0
本题很简单,看样例就能猜出题目所求是什么。
下面大致说下我的思路吧,假如某个点是“O”,就从这个点为起点开始查找。
看能不能找到符合条件的图形,另外,八个方向只需要找四个就可以了,至于为什么,自己试着思考一下。
下面是AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char a[20][20];int check(int i,int j,int k,int l,int m){    if(i<0||i>=15||j<0||j>=15||k<0||k>=15||l<0||l>=15||m<0||m>15)    {        return 0;    }    return 1;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        for(int i=0;i<15;i++)        {            scanf("%s",&a[i]);        }        int sum=0;        for(int i=0;i<15;i++)        {            for(int j=0;j<15;j++)            {                if(a[i][j]=='O')                {                    if(check(i-1,i+1,i+2,i+3,i+4)&&a[i-1][j]=='+'&&a[i+1][j]=='O'&&a[i+2][j]=='O'&&a[i+3][j]=='O'&&a[i+4][j]=='+')                    {                        sum++;                    }                    if(check(j-1,j+1,j+2,j+3,j+4)&&a[i][j-1]=='+'&&a[i][j+1]=='O'&&a[i][j+2]=='O'&&a[i][j+3]=='O'&&a[i][j+4]=='+')                    {                        sum++;                    }                    if(check(i+1,j-1,i-1,j+1,i-2)&&check(j+2,i-3,j+3,i-4,j+4)&&a[i+1][j-1]=='+'&&a[i-1][j+1]=='O'&&a[i-2][j+2]=='O'&&a[i-3][j+3]=='O'&&a[i-4][j+4]=='+')                    {                        sum++;                    }                    if(check(i-1,j-1,i+1,j+1,i+2)&&check(j+2,i+3,j+3,i+4,j+4)&&a[i-1][j-1]=='+'&&a[i+1][j+1]=='O'&&a[i+2][j+2]=='O'&&a[i+3][j+3]=='O'&&a[i+4][j+4]=='+')                    {                        sum++;                    }                }            }        }        printf("%d\n",sum);    }    return 0;}
0 0
原创粉丝点击