您的位置:首页 > 其它

poj2965-poj2965-The Pilots Brothers' refrigerator

2009-03-26 11:07 267 查看
方法同poj1753,但用在这题就TLE了,以下是TLE版本:

#include <stdio.h>

2#include <stdlib.h>

3#include <string.h>

4#define MAXSTATE 65536

5#define MAXSIZE 16

6#define ALLOPEN 0

7

8//队列结构体

9//全局变量

18QUEUE queue;

19int distance[MAXSTATE];

20int position[MAXSTATE];

21int parentState[MAXSTATE];

22//入队列函数

23void EnQueue(QUEUE *q, int d)

24//出队列函数

38int DeQueue(QUEUE *q)

39//判断队列是否为空

53

59int ChangeState(int id, int pos)

60

94void Output(int stat)

95

void main(int argc, char *argv[])

#include <stdio.h>

2#include <stdlib.h>

3#include <string.h>

4#define MAXSTATE 65536

5#define MAXSIZE 16

6#define ALLOPEN 0

7//循环队列结构体

8//队列函数

13void Init(QUEUE *q)

14int Empty(QUEUE *q)

18void EnQueue(QUEUE *q ,int d)

23int DeQueue(QUEUE *q)

32//全局变量

43int distance[MAXSTATE];

44int position[MAXSTATE];

45int parentState[MAXSTATE];

46QUEUE queue;

47

53

54int ChangeState(int id, int pos)

55

61void Output(int stat)

62

78void main(int argc, char *argv[])

79{

80 char c;

81 int cur_state=0, new_state;

82 int i=0;

83 QUEUE *q;

84 q=&queue;

85 //freopen("input.txt", "r", stdin);

86

87 while(scanf("%c", &c)!=EOF){

88 if(c!='\n'){

89 cur_state<<=1;

90 if (c=='+') cur_state+=1;

91 }

92 }

93 memset(distance, -1, MAXSTATE*sizeof(int));

94

95 if(cur_state==ALLOPEN){

96 printf("0\n");

97 return;

98 }

99 parentState[cur_state]=-1;

distance[cur_state]=0;

position[cur_state]=-1;

EnQueue(q, cur_state);

while(!Empty(q)){

cur_state=DeQueue(q);

for (i=0;i<MAXSIZE;i++){

new_state=ChangeState(cur_state, i);

if (new_state==ALLOPEN){//成功

parentState[new_state]=cur_state;

position[new_state]=i;

distance[new_state]=distance[cur_state]+1;

Output(new_state);

return;

}

if (distance[new_state]==-1){//未访问过结点

distance[new_state]=distance[cur_state]+1;

position[new_state]=i;

parentState[new_state]=cur_state;

EnQueue(q, new_state);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: