链接:
题意:
You have a given picture with size w×h. Determine if the given picture has a single "+" shape or not. A "+" shape is described below:
A "+" shape has one center nonempty cell.
There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction. All other cells are empty. Find out if the given picture has single "+" shape.思路:
遍历每一个点,当某个点的上下左右都为时。向四个方向扩展,记录的个数。之后结束遍历。 当某个点可以形成+且的个数等于所有*的个数的时候,YES。
代码:
#includeusing namespace std;typedef long long LL;const int MAXN = 1e3 + 10;const int MOD = 1e9 + 7;int n, m, k, t;char pi[MAXN][MAXN];int main(){ scanf("%d %d", &n, &m); getchar(); int sum = 0; for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { scanf("%c", &pi[i][j]); if (pi[i][j] == '*') sum++; } getchar(); } bool flag = false; for (int i = 2;i <= n-1;i++) { for (int j = 2;j <= m-1;j++) { if (pi[i][j]=='*'&&pi[i-1][j]=='*'&&pi[i][j+1]=='*'&&pi[i+1][j]=='*'&&pi[i][j-1]=='*') { flag = true; sum--; int tx, ty; tx = i-1, ty = j; while (tx >= 1 && pi[tx][ty] == '*') tx--, sum--; tx = i, ty = j+1; while (ty <= m && pi[tx][ty] == '*') ty++, sum--; tx = i+1, ty = j; while (tx <= n && pi[tx][ty] == '*') tx++, sum--; tx = i, ty = j-1; while (ty >= 1 && pi[tx][ty] == '*') ty--, sum--; } if (flag) break; } cerr << endl; if (flag) break; } if (flag && sum == 0) printf("YES\n"); else printf("NO\n"); return 0;}