一个游戏
http://poj.org/problem?id=1414
题目大概长成这样
Suppose that the turn of the player '4' comes now. If he puts his stone on the vertex shown in Figure 3a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 3b). The player gains 6 points, because the 6 stones of others are removed from the board (See Figure 3c).
As another example, suppose that the turn of the player '2' comes in Figure 2. If the player puts his
stone on the vertex shown in Figure 4a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 4b). The player gains 4 points, because the 4 stones of others are removed. But, at the same time, he loses 3 points, because his 3 stones are removed. As the result, the player's points of this turn is 4 ? 3 = 1 (See Figure 4c).
大概就是 选手N 往里面输入N的时候,(只能在0上输入)造成了后果:如果一群相同的数没有和0挨着,就全挂了,然后根据挂了的数和输入的数是否一样给编号为N的选手加分或减分。如果和0挨着,就不动。 问最大能得多少分。
就是挨个0搜索尝试,思路比较清晰,就是好墨迹啊- -!
#include <cstdio>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <math.h>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int board[15][15];
int n,c;
int vis[15][15]; //0未标记
int m_ans;
int fx[6]={ 0,-1,-1, 0, 1, 1};
int fy[6]={-1,-1, 0, 1, 1, 0};
int cnt;
struct p
{
int x;
int y;
};
int bfs(int x,int y)
{
int find_0=0;
queue <p> q;
p now,next;
now.x=x , now.y=y;
q.push(now);
int num=1;
vis[x][y]=1;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<6;i++){
int nx=now.x+fx[i];
int ny=now.y+fy[i];
if(board[nx][ny]==board[x][y]&&vis[nx][ny]==0)//相同,未走过
{
//printf("zuobiao %d %d\n",nx,ny );
vis[nx][ny]=1;
next.x=nx;
next.y=ny;
q.push(next);
num++;
}
if(board[nx][ny]==0) //找到empty
{
//printf("zuobiao %d %d ",nx,ny );
//printf("board[nx][ny]==0\n");
find_0=1; //这里如果直接return 0的话,有的数可能被搜索第二遍,并且找不到0了
}
}
}
if(find_0) return 0;
return num;
}
int solve(int x,int y)
{
memset(vis,0,sizeof(vis)); //应该在这里重置- -
cnt=0;
cnt-=bfs(x,y);//找自己
//cout <<cnt<<'a'<<endl;
for(int k=0;k<6;k++)
{
int nx=x+fx[k];
int ny=y+fy[k];
if(board[nx][ny]!=-1&&vis[nx][ny]==0&&board[nx][ny]!=0)//在界内 并且和c不同 没有被搜索过
{
cnt+=bfs(nx,ny);
}
}
return cnt;
}
int main()
{
//freopen("input.txt","r",stdin);
while(cin>>n>>c,n)
{
m_ans=-INF;
memset(board,-1,sizeof(board));
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>board[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
if(board[i][j]==0){
board[i][j]=c;
m_ans=max(m_ans,solve(i,j));
board[i][j]=0;
}
cout<<m_ans<<endl;
}
return 0;
}