Codeforces Round #644 (Div. 3) E. Polygon (思维)

题目链接
题意:
给你一个n*n的方阵,每行每列都有一个大炮
Codeforces Round #644 (Div. 3) E. Polygon (思维)_第1张图片
最开始,方阵中全是0,炮弹发射数字1的炮弹。但是现在如果这个炮弹会一直向前发现,直到越到前面是方阵的边界后停下,或者他的前面一个数是1,则也停下。
Codeforces Round #644 (Div. 3) E. Polygon (思维)_第2张图片
现在给你一个方阵中的情况,让你判断这个方阵是否可能由上面的条件所形成。

思路:
根据题意,每一个1的下方或者右方不是边界就是1。如果都不是,则不可能形成这个矩阵。

AC代码

#include 
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n,ans = 1;
        cin >> n;
        char str[n+5][n+5];
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)
                cin >> str[i][j];
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                if(str[i][j] == '1')
                {
                    if(i == n || j == n)
                        continue;
                    if(str[i][j+1] == '1' || str[i+1][j] == '1')
                        continue;
                    ans = 0;
                }
            }
        }
        ans ? cout << "YES" << endl : cout << "NO" << endl;
    }
}

你可能感兴趣的:(codeforces,思维)