夏夜、

心若平似镜、何题不AC。

UESTC 1698 The Game

数据量这么小,怎么写都行。。

枚举每一个为‘.'的格子,从这个格子开始搜索能到达的数字格子,两个格子交换一下,判断连成几个,再交换回来,继续搜索下一个。。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb(a) push_back(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!=' ')return ch;
    }
    return EOF;
}
char grid[11][11];
int maxx;
int vis[11][11];
int dfsflag[11][11];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int check(int x,int y,int v)
{
    int sum=0;
    int num;
    num=0;
    for(int i=x+1;i<=10;i++)
        if(grid[i][y]==v+'0')num++;
        else break;
    for(int i=x-1;i>=1;i--)
        if(grid[i][y]==v+'0')num++;
        else break;
    if(num>=4)
        sum+=num;
    num=0;
    for(int j=y+1;j<=10;j++)
        if(grid[x][j]==v+'0')num++;
        else break;
    for(int j=y-1;j>=1;j--)
        if(grid[x][j]==v+'0')num++;
        else break;
    if(num>=4)
        sum+=num;
    num=0;
    for(int i=x+1,j=y+1;i<=10&&j<=10;i++,j++)
        if(grid[i][j]==v+'0')num++;
        else break;
    for(int i=x-1,j=y-1;i>=1&&j>=1;i--,j--)
        if(grid[i][j]==v+'0')num++;
        else break;
    if(num>=4)
        sum+=num;
    num=0;
    for(int i=x+1,j=y-1;i<=10&&j>=1;i++,j--)
        if(grid[i][j]==v+'0')num++;
        else break;
    for(int i=x-1,j=y+1;i>=1&&j<=10;i--,j++)
        if(grid[i][j]==v+'0')num++;
        else break;
    if(num>=4)
        sum+=num;
    return sum;
}
int dfs(int x,int y,int bx,int by)
{
    vis[x][y]=1;
    if(grid[x][y]!='.')
    {
        int v=grid[x][y]-'0';
        grid[x][y]='.';
        int d=check(bx,by,v);
        grid[x][y]=v+'0';
        if(d==0)return 0;
        maxx=max(d+1,maxx);
        return 0;
    }
    for(int d=0;d<4;d++)
    {
        int nx=x+dx[d],ny=y+dy[d];
        if(nx>=1&&nx<=10&&ny>=1&&ny<=10&&!vis[nx][ny])
            dfs(nx,ny,bx,by);
    }
}
int judge(int x,int y)
{
    memset(vis,0,sizeof(vis));
    dfs(x,y,x,y);
}
int main()
{
   // freopen("d:\\in.txt","r",stdin);
    int ca=0;
    while(scanf("%s",grid[1]+1)!=EOF)
    {
        for(int i=2;i<=10;i++)
            scanf("%s",grid[i]+1);
        if(ca!=0)printf("\n");
        int flag=0;
        for(int i=1;i<=10;i++)
        {
            for(int j=1;j<=10;j++)if(grid[i][j]!='.')
            {
                int d=check(i,j,grid[i][j]-'0');
                if(d!=0)
                {
                    flag=1;break;
                }
            }
            if(flag)break;
        }
        if(flag)
        {
            printf("Case #%d: Waiting!\n",++ca);
            continue;
        }
        maxx=0;
        for(int i=1;i<=10;i++)
        {
            for(int j=1;j<=10;j++)if(grid[i][j]=='.')
            {
                judge(i,j);
            }
        }
        printf("Case #%d: %d\n",++ca,maxx);
    }
    return 0;
}
View Code

 

posted on 2013-08-20 20:55  BMan、  阅读(218)  评论(0编辑  收藏  举报

导航