《ACM书中题目》 W

来源:互联网 发布:乔丹荣誉数据 编辑:程序博客网 时间:2024/06/05 16:55
  • 原题

    Description

    Tom’s Meadow
    Tom has a meadow in his garden. He divides it into N * M squares. Initially all the squares were covered with grass. He mowed down the grass on some of the squares and thinks the meadow is beautiful if and only if

    Not all squares are covered with grass.
    No two mowed squares are adjacent.
    Two squares are adjacent if they share an edge. Here comes the problem: Is Tom’s meadow beautiful now?

    Input
    The input contains multiple test cases!

    Each test case starts with a line containing two integers N, M (1 <= N, M <= 10) separated by a space. There follows the description of Tom’s Meadow. There’re N lines each consisting of M integers separated by a space. 0(zero) means the corresponding position of the meadow is mowed and 1(one) means the square is covered by grass.

    A line with N = 0 and M = 0 signals the end of the input, which should not be processed

    Output
    One line for each test case.

    Output “Yes” (without quotations) if the meadow is beautiful, otherwise “No”(without quotations).

  • 理解&思路
    判断一个给定矩阵是否“漂亮”,漂亮的条件是:
    1、不能全是1
    2、两个0不能挨着
    定义一个变量初始化为1,在输入过程中进行判断,如果输入有0,则该标识变为0,表示矩阵不全为1。 之后再逐行、行间判断是否有两个挨着的0。

  • AC代码

#include<bits/stdc++.h>using namespace std;int main(){    int p[10][10];    int n,m,i,j,k,flg;    while(cin>>n>>m&&n!=0&&m!=0)    {        flg=1;        for(i=0;i<n;i++)            for(j=0;j<m;j++)            {                cin>>p[i][j];                if(p[i][j]==0)flg=0;            }        if(flg)        {            cout<<"No"<<endl;            goto RL;        }        for(i=1;i<m;i++)        {            if(p[0][i]==0&&p[0][i-1]==0)            {                cout<<"No"<<endl;                goto RL;            }        }        for(i=1;i<n;i++)        {            for(j=0;j<m;j++)            {                if(p[i][j]==0&&p[i-1][j]==0)                {                    cout<<"No"<<endl;                    goto RL;                }                if(j!=0)                {                    if(p[i][j]==0&&p[i][j-1]==0)                    {                        cout<<"No"<<endl;                        goto RL;                    }                }            }        }        cout<<"Yes"<<endl;        continue;        RL:continue;    }}
  • 总结
    开始把题意理解错了,当成了两个未修剪的草坪也不能挨着。
    代码:
#include<bits/stdc++.h>using namespace std;int main(){    int p[10][10];    int n,m,i,j,k;    while(cin>>n>>m&&n!=0&&m!=0)    {        for(i=0;i<n;i++)            for(j=0;j<m;j++)                cin>>p[i][j];        for(i=1;i<m;i++)        {            if(p[0][i]==0&&p[0][i-1]==0||p[0][i]==1&&p[0][i-1]==1)            {                cout<<"No"<<endl;                goto RL;            }        }        for(i=1;i<n;i++)        {            for(j=0;j<m;j++)            {                if(p[i][j]==0&&p[i-1][j]==0||p[i][j]==1&&p[i-1][j]==1)                {                    cout<<"No"<<endl;                    goto RL;                }                if(j!=0)                {                    if(p[i][j]==0&&p[i][j-1]==0||p[i][j]==1&&p[i][j-1]==1)                    {                        cout<<"No"<<endl;                        goto RL;                    }                }            }        }        cout<<"Yes"<<endl;        continue;        RL:continue;    }}
0 0
原创粉丝点击