题目见:http://soj.sysu.edu.cn/7151
乍一看是搜索,其实仔细看完后就会发现是模拟。
针对不同“智商”,分情况处理即可,对每读入的字符都进行移动。
源代码如下:
#include <stdio.h>
void find_position(char dir, int power, char na[]);
int n, m, x, y;
int main() {
int k, i, j, p;
char ch;
char name[300] = {0};
while (scanf("%d %d %d", &n, &m, &k) && n != 0) {
for (i = 1; i <= k; i++) {
scanf("%s", name);
scanf("%d %d %d", &x, &y, &p);
getchar();
for (; ;) {
ch = getchar();
find_position(ch, p, name);
if (ch == '\n') break;
}
}
printf("\n");
}
return 0;
}
void find_position(char dir, int power, char na[]) {
if (dir == '\n') {
if (x >= n || x < 0 || y >= m || y < 0)
printf("%s is out of square!\n", na);
else printf("%d %d\n", x, y);
return;
}
if (power < 0 && (x >= n || x < 0 || y >= m || y < 0)) return;
switch(dir) {
case 'N' :
y++;
break;
case 'S' :
y--;
break;
case 'E' :
x++;
break;
case 'W' :
x--;
break;
}
if (power > 0) {
if (x >= n) x = 0;
if (x < 0) x = n - 1;
if (y >= m) y = 0;
if (y < 0) y = m - 1;
} else if (power == 0) {
if (x >= n) x--;
if (x < 0) x++;
if (y >= m) y--;
if (y < 0) y++;
}
return;
}