Tag Archives: 指针

[ACM] POJ1573解题报告

夜里边切题边等电影缓冲真是太爽了..
正在慢慢的切某人切不下来的题..
1573挺简单的 以后这种模拟的数组我都从1开始就好了..省得墨迹墨迹的整的头大..

主要就是想到一个监视指针的方法..做个tester指针数组 对应map上面所有的单元
这样本来看不懂的指针地址(0x22fb18这样)就能通过查找tester来获得它的位置(tester是一个地址的数组 找到对应的 然后数数)

详见下图 ap是tester,p==0x22fb18,在ap中排第10个,则p==*(ap[9])

1#include <cstdio>
2#include <cstring>
3typedef struct spot {
4    char c;
5    bool b;
6    int n;
7} Spot;
8Spot *p;
9 
10void move() {
11    switch(p->c) {
12        case 'N':
13            p-=12;
14            break;
15        case 'S':
16            p+=12;
17            break;
18        case 'E':
19            p+=1;
20            break;
21        case 'W':
22            p-=1;
23    }
24}
25 
26int main() {
27    int i,enter,j,n,rows,cols;
28    Spot a[12][12];
29    /*Tester
30    Spot *(ap[3][6]);
31    for(i=0;i<3;i++)
32        for(j=0;j<7;j++)
33            ap[i][j]=a[i]+j;*/
34    while(1) {
35        for(i=0;i<144;i++) {
36            ((Spot *)a+i)->c=-1;
37            (a[0]+i)->b=false;
38        }
39        n=0;
40        scanf("%d%d%d",&rows,&cols,&enter);
41        if(!rows&&!cols&&!enter)
42            return 0;
43        getchar();
44        for(i=1;i<=rows;i++) {
45            for(j=1;j<=cols;j++)
46                a[i][j].c=getchar();
47            getchar();
48        }
49        p=a[1]+enter;
50        /* TESTER
51        for(i=0;i<rows;i++) {
52            for(j=1;j<=cols;j++)
53                printf("%c",a[i][j].c);
54            printf("\n");
55        } */
56        while(1) {
57            p->b=true;
58            p->n=n;
59            if(p->c==-1) {
60                printf("%d step(s) to exit\n",n);
61                break;
62            }
63            move();
64            if(p->b) {
65                printf("%d step(s) before a loop of %d step(s)\n",p->n,n+1-p->n);
66                break;
67            }
68            n++;
69        }
70    }
71    return 0;
72}