HDU-2074 题解
叠筐
题目大意
需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。
Time: 1000 ms
Memory: 32768 kB
解题思路及分析
可以模拟,也可以用bfs做
这里用bfs
n=1的时候要特判,注意格式
(被PE卡了12次,最后发现是n=1特判没加continue,,)
AC代码
#include <bits/stdc++.h>
using namespace std;
int n;
char clr[2];
char mp[100][100];
bool vis[100][100];
bool judge(int x, int y)
{
return 0 <= x && x < n && 0 <= y && y < n;
}
void bfs(int xxx, int yyy)
{
queue<int> qx;
queue<int> qy;
queue<int> qi;
qx.push(xxx);
qy.push(yyy);
qi.push(0);
while (!qx.empty())
{
int x = qx.front();
int y = qy.front();
int i = qi.front();
qx.pop();
qy.pop();
qi.pop();
mp[x][y] = clr[i];
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
if (dx == dy && dy == 0)
continue;
int xx = x + dx;
int yy = y + dy;
if (judge(xx, yy) && !vis[xx][yy])
{
qx.push(xx);
qy.push(yy);
qi.push(1 - i);
vis[xx][yy] = 1;
}
}
}
}
}
int main()
{
int f = 1;
while (~scanf("%d %c %c", &n, &clr[0], &clr[1]))
{
if (f) f = 0;
else printf("\n");
if (n == 1)
{
printf("%c\n", clr[0]);
continue;
}
memset(vis, 0, sizeof(vis));
bfs(n / 2, n / 2);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ( (i == 0 && j == 0)
|| (i == 0 && j == n - 1)
|| (i == n-1 && j == 0)
|| (i == n-1 && j == n-1)
)
{
printf(" ");
}
else
{
printf("%c", mp[i][j]);
}
}
printf("\n");
}
}
return 0;
}