http://acm.hdu.edu.cn/showproblem.php?pid=2258
其实就是一道模拟题
注意题目是往右移~~刚才做了个往左移~~导致一致wa~~~
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#define BUG1 printf("here!\n")
#define BUG2 printf("herasdfe!\n")
using namespace std;
struct node
{
char num[35][35];
int score;
};
struct point
{
int x,y;
};
struct node1
{
point num[405];
int count;
};
int vis[35][35];
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
node1 maxx,now;
int n,m;
node update(node cur,int cc)
{
int i,j;
node temp;
temp.score=cc+cur.score;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
temp.num[i][j]='0';
}
for(i=0;i<m;i++)
{
int k=n-1;
for(j=n-1;j>=0;j--)
{
if(cur.num[j][i]!='0')
{
temp.num[k--][i]=cur.num[j][i];
}
}
}
int k=m-1;
for(i=m-1;i>=0;i--)
{
if(temp.num[n-1][i]!='0')
{
int j;
for(j=n-1;j>=0;j--)
{
temp.num[j][k]=temp.num[j][i];
}
k--;
}
}
while(k!=-1)
{
int j;
for(j=n-1;j>=0;j--)
{
temp.num[j][k]='0';
}
k--;
}
return temp;
}
void work(node cur,int x,int y)
{
queue<int> q;
vis[x][y]=1;
q.push(x*m+y);
int flag=cur.num[x][y];
memset(now.num,0,sizeof(now.num));
now.num[0].x=x;
now.num[0].y=y;
now.count=1;
while(!q.empty())
{
int c=q.front();
q.pop();
int x=c/m;
int y=c%m;
int i;
for(i=0;i<4;i++)
{
int nx=x+xx[i];
int ny=y+yy[i];
if(nx<0||nx>=n||ny<0||ny>=m)
continue;
if(cur.num[nx][ny]==flag&&!vis[nx][ny])
{
vis[nx][ny]=1;
now.num[now.count].x=nx;
now.num[now.count].y=ny;
now.count++;
q.push(nx*m+ny);
//printf("%d %d\n",nx,ny);
}
}
}
if(now.count>maxx.count)
{
maxx=now;
}
else if(now.count==maxx.count)
{
if(now.num[0].x<maxx.num[0].x||(now.num[0].x==maxx.num[0].x&&now.num[0].y<maxx.num[0].y))
maxx=now;
}
}
int bfs(node cur)
{
queue<node> q;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
int i,j;
maxx.count=-1;
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(vis[i][j]||cur.num[i][j]=='0')
continue;
else
work(cur,i,j);
}
}
if(maxx.count==1||maxx.count==-1)
return cur.score;
for(i=0;i<maxx.count;i++)
{
cur.num[maxx.num[i].x][maxx.num[i].y]='0';
}
cur=update(cur,maxx.count*(maxx.count-1));
q.push(cur);
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
node temp;
memset(temp.num,0,sizeof(temp.num));
temp.score=0;
for(i=0;i<n;i++)
{
scanf("%s",temp.num[i]);
}
int res=bfs(temp);
if(res==-1)
printf("0\n");
else
printf("%d\n",res);
}
}