/*
* main.c
*
* Created on: Nov 30, 2012
* Author: mfb
*
* Example input:
* 5 5 2 2 0 0
* y0 . . . . .
* y1 a a a a .
* y2 . . . a .
* y3 . a a a .
* y4 . . . . .
*
* x> 0 1 2 3 4
* I don't know why when I enter these to the program it comes to 4,4 and stops.
*/
#include <stdio.h>
int spx, spy, fx, fy, xsize, ysize;
char *inmap, *check;
int input(void) {
scanf(" %d %d %d %d %d %d", &xsize, &ysize, &fx, &fy, &spx, &spy);
inmap = (char *) malloc(xsize * ysize * sizeof(char));
check = (char *) malloc(xsize * ysize * sizeof(char));
int y;
for (y = 0; y < xsize * ysize; y++)
*(check + y) = 0;
char o;
for (y = 0; y < ysize * xsize; y++) {
scanf(" %c", &o);
if (o == '.')
*(inmap + y) = 1;
else
*(inmap + y) = 0;
}
return 0;
}
int itaw(char *map, int fpx, int fpy) {
if (*(check + fpy * xsize + fpx) == 1)
return 0;
else
*(check + fpy * xsize + fpx) = 1;
if (fpx >= xsize || fpy >= ysize || fpx < 0 || fpy < 0)
return 0;
if (*(map + fpy * xsize + fpx) == 0 || *(map + spy * xsize + spx) == 0)
return 0;
printf("(%d,%d)\n", fpx, fpy);
if (fpx == spx && fpy == spy)
return 1;
return (itaw(map, fpx - 1, fpy) || itaw(map, fpx, fpy - 1)
|| itaw(map, fpx + 1, fpy) || itaw(map, fpx, fpy + 1));
}
int main(void) {
input();
int result = itaw(inmap, fx, fy);
printf("%d\n", result);
return 0;
}
以上是一个找到两点之间的方法。 ''意味着方式,所有其他字符意味着墙。 它主要起作用,但当我输入上面写的内容时,它返回0。
答案 0 :(得分:5)
问题是在访问变量之前不进行边界检查。在访问check
之前,您必须确保它们大于0且小于最大值。我认为这是正确的版本:
int itaw(char *map, int fpx, int fpy) {
if (fpx >= xsize || fpy >= ysize || fpx < 0 || fpy < 0)
return 0;
if (*(check + fpy * xsize + fpx) == 1)
return 0;
else
*(check + fpy * xsize + fpx) = 1;