zoukankan      html  css  js  c++  java
  • 洛谷P1331 海战

    海战

    题目链接

    这还是一道联通块的题,只是需要判断是否存在以下四种情况:
    o. .o oo oo
    oo oo o. .o
    如果存在就是Bad placement.
    要注意标记以下,不然会出现多次输出Bad placement.的情况。
    AC代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #define MAXN 10010
    using namespace std;
    char G[MAXN][MAXN];
    int vis[MAXN][MAXN];
    int cnt;
    int color;
    struct item 
    {
        int x;
        int y;
    };
    int R,C;
    queue<item>q;
    void bfs(item t)
    {
        q.push(t);
        vis[t.x][t.y]=color;
        while(!q.empty())
        {
            item r;
            r=q.front();
            q.pop();
            //if(vis[r.x][r.y]!=0) continue;
            if(G[r.x+1][r.y]=='#'&&r.x+1<=R&&vis[r.x+1][r.y]==0)
            {
                vis[r.x+1][r.y]=color;
                item t2;
                t2.x=r.x+1;
                t2.y=r.y;
                q.push(t2); 
            }
            if(G[r.x-1][r.y]=='#'&&r.x-1>=1&&vis[r.x-1][r.y]==0)
            {
                vis[r.x-1][r.y]=color;
                item t2;
                t2.x=r.x-1;
                t2.y=r.y;
                q.push(t2);
            }
            if(G[r.x][r.y+1]=='#'&&r.y+1<=C&&vis[r.x][r.y+1]==0)
            {
                vis[r.x][r.y+1]=color;
                item t2;
                t2.x=r.x;
                t2.y=r.y+1;
                q.push(t2);
            }
            if(G[r.x][r.y-1]=='#'&&r.y-1>=1&&vis[r.x][r.y-1]==0)
            {
                vis[r.x][r.y-1]=color;
                item t2;
                t2.x=r.x;
                t2.y=r.y-1;
                q.push(t2);
            }
        }
    }
    int main()
    {
        scanf("%d%d",&R,&C);
        for(int i=1;i<=R;i++)
        {
            for(int j=1;j<=C;j++)
            {
                cin>>G[i][j];
            }
        }
        item t;
        for(int i=1;i<=R;i++)
        {
            for(int j=1;j<=C;j++)
            {
                if(G[i][j]=='#')
                {
                    t.x=i;
                    t.y=j;
                    if(vis[i][j]==0)
                    {
                        cnt++;
                        color++;
                        bfs(t);
                    }
                }
            }
        }
        bool sign=0;
        for(int i=1;i<=R;i++)
        {
            for(int j=1;j<=C;j++)
            {
                if(G[i][j]=='#'&&G[i+1][j]=='#'&&!sign&&G[i][j+1]=='#'&&G[i+1][j+1]=='.')
                {
                    printf("Bad placement.");
                    sign=1;
                }
                else if(G[i][j]=='#'&&G[i+1][j]=='#'&&!sign&&G[i][j+1]=='.'&&G[i+1][j+1]=='#')
                {
                    printf("Bad placement.");
                    sign=1;
                }
                else if(G[i][j]=='#'&&G[i+1][j]=='.'&&!sign&&G[i][j+1]=='#'&&G[i+1][j+1]=='#')
                {
                    printf("Bad placement.");
                    sign=1;
                }
                else if(G[i][j]=='.'&&G[i+1][j]=='#'&&!sign&&G[i][j+1]=='#'&&G[i+1][j+1]=='#')
                {
                    printf("Bad placement.");
                    sign=1;
                }
            }
        } 
    //	for(int i=1;i<=R;i++)
    //	{
    //		for(int j=1;j<=C;j++)
    //		{
    //			cout<<G[i][j];
    //		}
    //		cout<<endl;;
    //	}
        if(!sign)
        {
            printf("There are %d ships.",color);
        }
    //	for(int i=1;i<=R;i++)
    //	{
    //		for(int j=1;j<=C;j++)
    //		{
    //			printf("%d ",vis[i][j]);
    //		}
    //		cout<<endl;;
    //	}
        return 0;
    }
    
  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/LITTLESUNwl/p/10687433.html
Copyright © 2011-2022 走看看