3406: [Usaco2009 Oct]Invasion of the Milkweed 乳草的入侵
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 204 Solved: 110
[ Submit][ Status][ Discuss]
Description
Input
第1行:4个由空格隔开的整数X,K Mx,My.
第2到第Y+1行:每行由X个字符描述草地.
Output
一个单独的整数表示最后一个不是大石块的格子被乳草占领的星期数
Sample Input
4 3 1 1
....
..*.
.**.
Sample Output
4
答案就是离(x, y)最远的点
数据很小直接暴力吧
注意行和列是反的
#include<stdio.h>
char str[105][105];
int flag[105][105], qx[80005], qy[80005];
int dir[8][2] = {1,0,0,1,-1,0,0,-1,1,1,-1,1,1,-1,-1,-1};
int main(void)
{
int n, m, x, y, i, j, k, cnt, ans;
scanf("%d%d%d%d", &m, &n, &y, &x);
for(i=1;i<=n;i++)
scanf("%s", str[i]+1);
x = n-x+1;
flag[x][y] = 1;
ans = 0;
while(1)
{
cnt = 0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(str[i][j]=='*')
continue;
if(flag[i][j])
{
for(k=0;k<=7;k++)
{
qx[++cnt] = i+dir[k][0], qy[cnt] = j+dir[k][1];
if(qx[cnt]==0 || qx[cnt]==n+1 || qy[cnt]==0 || qy[cnt]==m+1 || flag[qx[cnt]][qy[cnt]] || str[qx[cnt]][qy[cnt]]=='*')
cnt--;
}
}
}
}
if(cnt==0)
break;
for(i=1;i<=cnt;i++)
flag[qx[i]][qy[i]] = 1;
ans++;
}
printf("%d\n", ans);
}