提出詳細
ソースコード
#include <stdio.h>
typedef struct
{
int x;
int y;
int before;
} save;
save s[1000];
int main(void)
{
int T = 0;
int anslist[10000] = {0};
int answercount = 0;
scanf("%d", &T);
for (int loop; loop < T; loop++)
{
int W = 0, H = 0;
int board[11][11] = {0}; //y,x
int x = 0, y = 0;
int count = 0;
int scount = 0;
int anscount = 0;
scanf("%d %d", &W, &H);
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
scanf("%d", &board[i][j]);
if (board[i][j] == 1)
{
x = j;
y = i;
}
}
}
s[count].x = x;
s[count].y = y;
count++;
while (1)
{
x = s[scount].x;
y = s[scount].y;
if (board[y][x] == 2)
{
while (1)
{
scount = s[scount].before;
//printf("ans=%d %d\n", s[scount].x, s[scount].y);
anscount++;
if (scount == 0)
{
anslist[answercount] = anscount;
answercount++;
break;
}
}
break;
}
//printf("%d %d\n", x, y);
if (x - 1 >= 0 && board[y][x - 1] != 3)
{
int flag = 0;
for (int i = 0; i < count; i++)
{
if (s[i].x == x - 1 && s[i].y == y)
{
flag = 1;
break;
}
}
if (flag != 1)
{
s[count].x = x - 1;
s[count].y = y;
s[count].before = scount;
count++;
}
}
if (x + 1 < W && board[y][x + 1] != 3)
{
int flag = 0;
for (int i = 0; i < count; i++)
{
if (s[i].x == x + 1 && s[i].y == y)
{
flag = 1;
break;
}
}
if (flag != 1)
{
s[count].x = x + 1;
s[count].y = y;
s[count].before = scount;
count++;
}
}
if (y - 1 >= 0 && board[y - 1][x] != 3)
{
int flag = 0;
for (int i = 0; i < count; i++)
{
if (s[i].x == x && s[i].y == y - 1)
{
flag = 1;
break;
}
}
if (flag != 1)
{
s[count].x = x;
s[count].y = y - 1;
s[count].before = scount;
count++;
}
}
if (y + 1 < H && board[y + 1][x] != 3)
{
int flag = 0;
for (int i = 0; i < count; i++)
{
if (s[i].x == x && s[i].y == y + 1)
{
flag = 1;
break;
}
}
if (flag != 1)
{
s[count].x = x;
s[count].y = y + 1;
s[count].before = scount;
count++;
}
}
scount++;
if (scount == count)
{
anslist[answercount] = 0;
answercount++;
break;
}
}
}
for (int i = 0; i < answercount; i++)
{
printf("Case #%d:\n", i + 1);
printf("%d\n", anslist[i]);
}
return 0;
}
提出情報
提出出力結果
テストケース情報