社区讨论
P1331 海战:求助!!
学术版参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lo8j8caz
- 此快照首次捕获于
- 2023/10/27 19:30 2 年前
- 此快照最后确认于
- 2023/10/27 19:30 2 年前
CPP
#include <iostream>
using namespace std;
int a[1005][1005], m, n, cnt = 0;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
void dfs(int x, int y)
{
a[x][y] = 0;
for(int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if(tx > 0 && tx <= m && ty > 0 && ty <=n && a[tx][ty])
{
//a[tx][ty] = 0;
dfs(tx, ty);
}
}
}
bool d(int i,int j)
{
int c = 0;
if(a[i][j]) c++;
if(a[i+1][j]) c++;
if(a[i][j+1]) c++;
if(a[i+1][j+1]) c++;
if(c == 3) return 0;
return 1;
}
int main()
{
cin >> m >> n;
char ch;
getchar();
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
ch = getchar();
if(ch == '.')
{
a[i][j] = 0;
}
else
{
a[i][j] = 1;
}
}
getchar();
}
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(i < m && j < n && !d(i, j))
{
cout << "Bad placement.";
return 0;
}
}
}
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[i][j])
{
// a[i][j] = 0;
dfs(i, j);
cnt++;
}
}
}
cout << "There are " << cnt << " ships.";
return 0;
}
各位大佬,各路神仙,帮忙一看呗~~
回复
共 1 条回复,欢迎继续交流。
正在加载回复...