http://codeforces.com/contest/745/problem/B
题目要求的是,给定一个图形,要求里面判断是否有矩形,且仅有一个
就是
XXX....
XXX...X
是不行的,因为有两个了。

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> int n, m; const int maxn = 2000 + 20; char str[maxn][maxn]; char sub[maxn][maxn]; int tonext[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; bool vis[maxn][maxn]; void dfs(int x, int y) { for (int i = 0; i < 4; ++i) { int tx = x + tonext[i][0]; int ty = y + tonext[i][1]; if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty] && str[tx][ty] == 'X') { vis[tx][ty] = true; dfs(tx, ty); } } } bool check() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (str[i][j] == 'X' && !vis[i][j]) return false; } } return true; } void work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%s", str[i] + 1); } int ans = 0; for (int i = 1; i <= n; ++i) { bool flag1 = false; bool flag2 = false; bool be = false; for (int j = 1; j <= m; ++j) { if (str[i][j] == 'X') { if (str[i - 1][j] == 'X') { if (be && !flag1) { cout << "NO" << endl; return; } flag1 = true; } if (str[i + 1][j] == 'X') { if (be && !flag2) { cout << "NO" << endl; return; } flag2 = true; } be = true; } if (str[i][j] == 'X' && flag1) { if (str[i - 1][j] != 'X') { cout << "NO" << endl; return; } } if (str[i][j] == 'X' && flag2) { if (str[i + 1][j] != 'X') { cout << "NO" << endl; return; } } } } // cout << "ff" << endl; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (str[i][j] == 'X') { vis[i][j] = true; dfs(i, j); if (check()) { cout << "YES" << endl; } else cout << "NO" << endl; return; } } } } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif work(); return 0; }
感觉我判断起来有点麻烦啊