为什么本地有输出,交到洛谷上就是Too short?
#include <bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
int r, c, dirx[] = {0, 0, 1, -1}, diry[] = {1, -1, 0, 0};
char a[1005][1005];
struct point {
int x, y;
};
bool check(string s1, string s2) {
if (s1 == "##") {
if (s2 == "#." || s2 == ".#")
return 0;
else
return 1;
} else if (s1 == "..") {
if (s2 == "#." || s2 == ".#")
return 0;
else
return 1;
} else if (s1 == ".#") {
if (s2 == "#." && s2 == ".." && s2 == "##")
return 0;
else
return 1;
} else if (s1 == "#.") {
if (s2 == ".#" && s2 == ".." && s2 == "##")
return 0;
else
return 1;
}
string t = s1;
s1 = s2;
s2 = t;
if (s1 == "##") {
if (s2 == "#." || s2 == ".#")
return 0;
else
return 1;
} else if (s1 == "..") {
if (s2 == "#." || s2 == ".#")
return 0;
else
return 1;
} else if (s1 == ".#") {
if (s2 == "#." && s2 == ".." && s2 == "##")
return 0;
else
return 1;
} else if (s1 == "#.") {
if (s2 == ".#" && s2 == ".." && s2 == "##")
return 0;
else
return 1;
}
return 1;
}
void bfs(int sx, int sy) {
queue<point>q;
q.push({sx, sy});
while (!q.empty()) {
point now = q.front();
q.pop();
int x = now.x, y = now.y;
if (x < 1 || x > r)
continue;
if (y < 1 || y > c)
continue;
if (vis[x][y])
continue;
if (a[x][y] == '.')
continue;
vis[x][y] = true;
for (int i = 0; i < 4; i++)
q.push({x + dirx[i], y + diry[i]});
}
}
int main() {
int cnt = 0;
cin >> r >> c;
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
cin >> a[i][j];
for (int i = 1; i <= r - 1; i++) {
for (int j = 1; j <= c - 1; j++) {
string s1 = "", s2 = "" ;
for (int k = 1; k <= 2; k++) {
s1 = s1 + a[i][j + k];
s2 = s2 + a[i + 1][j + k];
}
if (!check(s1, s2)) {
cout << "Bad placement";
return 0;
}
}
}
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
if (!vis[i][j] && a[i][j] == '#') {
cnt++;
bfs(i, j);
}
printf("There are %d ships.", cnt);
return 0;
}