Submission #1417434


Source Code Expand

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
	int x;
	int y;
	int n;
} point_t;

static point_t *pop(void);
static void trypush(char map[51][51], char used[51][51], int y, int x, int n);
static void push(point_t *p);

static point_t *Stack[10000];
static int Index;

int main(int argc, char *argv[])
{
	char map[51][51], buf[512], used[51][51];
	int R, C, i, j;
	point_t *p, g;

	p = calloc(sizeof(point_t), 1);

	memset(used, 0, sizeof(used));

	scanf("%d %d", &R, &C);
	scanf("%d %d", &p->y, &p->x);
	scanf("%d %d\n", &g.y, &g.x);

	for ( i = 1; i <= R; i++ ){
		fgets(buf, sizeof(buf), stdin);
		for ( j = 1; j <= C; j++ ){
			map[i][j] = buf[j-1];
		}
	}

	while( p != NULL ){
		if ( p->x == g.x && p->y == g.y ){
			break;
		}
		trypush(map, used, p->y+1, p->x  , p->n);
		trypush(map, used, p->y-1, p->x  , p->n);
		trypush(map, used, p->y  , p->x+1, p->n);
		trypush(map, used, p->y  , p->x-1, p->n);
		free(p);
		p = pop();
	}
	printf("%d\n", p->n);

	return 0;
}

static void trypush(char map[51][51], char used[51][51], int y, int x, int n)
{
	point_t *p;
	if ( map[y][x] == '#' ){
		return;
	}
	if ( used[y][x] != 0 ){
		return;
	}
	used[y][x] = 1;
	p = calloc(sizeof(point_t), 1);
	p->y = y;
	p->x = x;
	p->n = n+1;
	push(p);
}

static void push(point_t *p)
{
	Stack[Index++] = p;
}

static point_t *pop(void)
{
	int i;
	point_t *ret;
	if ( Index == 0 ){
		return NULL;
	}
	ret = Stack[0];
	for ( i = 0; i < Index - 1; i++ ){
		Stack[i] = Stack[i+1];
	}
	Index--;
	return ret;
}

Submission Info

Submission Time
Task C - 幅優先探索
User shoji127
Language C (GCC 5.4.1)
Score 100
Code Size 1613 Byte
Status AC
Exec Time 1 ms
Memory 128 KB

Compile Error

./Main.c: In function ‘main’:
./Main.c:28:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &R, &C);
  ^
./Main.c:29:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &p->y, &p->x);
  ^
./Main.c:30:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d\n", &g.y, &g.x);
  ^
./Main.c:33:3: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result [-Wunused-result]
   fgets(buf, sizeof(buf), stdin);
   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
All subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 1 ms 128 KB
subtask0_sample02.txt AC 1 ms 128 KB
subtask0_sample03.txt AC 1 ms 128 KB
subtask1_01.txt AC 1 ms 128 KB
subtask1_02.txt AC 1 ms 128 KB
subtask1_03.txt AC 1 ms 128 KB
subtask1_04.txt AC 1 ms 128 KB
subtask1_05.txt AC 1 ms 128 KB
subtask1_06.txt AC 1 ms 128 KB
subtask1_07.txt AC 1 ms 128 KB
subtask1_08.txt AC 1 ms 128 KB
subtask1_09.txt AC 1 ms 128 KB
subtask1_10.txt AC 1 ms 128 KB
subtask1_11.txt AC 1 ms 128 KB
subtask1_12.txt AC 1 ms 128 KB
subtask1_13.txt AC 1 ms 128 KB
subtask1_14.txt AC 1 ms 128 KB
subtask1_15.txt AC 1 ms 128 KB
subtask1_16.txt AC 1 ms 128 KB
subtask1_17.txt AC 1 ms 128 KB
subtask1_18.txt AC 1 ms 128 KB
subtask1_19.txt AC 1 ms 128 KB
subtask1_20.txt AC 1 ms 128 KB
subtask1_21.txt AC 1 ms 128 KB
subtask1_22.txt AC 1 ms 128 KB