题目连接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1970
这道题实际是一个简单的搜索题目,对每个位置搜索水平、竖直、主对角线、副对角线进行可满足性搜索,主要处理好下标的对应关系!
#include <iostream>
using namespace std;
int map[19][19];
bool Search(int r,int c,int& rr,int& cc)
{
int i,j,count;
count = 1;
for(i = c-1;i >= 0 && map[r][i] == map[r][c];--i,++count);
rr = r,cc = i+1;
for(i = c+1;i < 19 && map[r][i] == map[r][c];++i,++count);
if(count == 5)
return true;
count = 1;
for(i = r-1;i >= 0 && map[i][c] == map[r][c];--i,++count);
rr = i+1,cc = c;
for(i = r+1;i < 19 && map[i][c] == map[r][c];++i,++count);
if(count == 5)
return true;
count = 1;
for(i = r+1,j = c-1;i < 19 && j >= 0 && map[i][j] == map[r][c];++i,--j,++count);
rr = i-1,cc = j+1;
for(i = r-1,j = c+1;i >= 0 && j < 19 && map[i][j] == map[r][c];--i,++j,++count);
if(count == 5)
return true;
count = 1;
for(i = r-1,j = c-1;i >= 0 && j >= 0 && map[i][j] == map[r][c];--i,--j,++count);
rr = i+1,cc = j+1;
for(i = r+1,j = c+1;i < 19 && j < 19 && map[i][j] == map[r][c];++i,++j,++count);
if(count == 5)
return true;
return false;
}
int main()
{
freopen("in.txt","r",stdin);
int i,j,r,c,t,s;
cin >> t;
while(t--)
{
s = 0;
for(i = 0;i < 19;++i)
{
for(j = 0;j < 19;++j)
cin >> map[i][j];
}
for(i = 0;i < 19 && s == 0;++i)
{
for(j = 0;j < 19;++j)
{
if(map[i][j] == 1)
{
if(Search(i,j,r,c))
{
s = 1;
break;
}
}
else if(map[i][j] == 2)
{
if(Search(i,j,r,c))
{
s = 2;
break;
}
}
}
}
cout << s << endl;
if(s != 0)
cout << r+1 << ' ' << c+1 << endl;
}
return 0;
}