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);
}
}
}
}
#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);
}
}
}
}
相关文章推荐
- poj2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- POJ2965 The Pilots Brothers' refrigerator (精妙方法秒杀DFS BFS)
- POJ2965-The Pilots Brothers' refrigerator
- 枚举 poj2965 The Pilots Brothers' refrigerator
- [POJ2965]The Pilots Brothers' refrigerator【枚举】
- POJ2965-The Pilots Brothers' refrigerator
- POJ2965《The Pilots Brothers' refrigerator》方法:DFS+Bit
- POJ2965----The Pilots Brothers' refrigerator
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- 暴力枚举-POJ2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator dfs
- 搜索- poj2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator 枚舉題目
- xor高斯消元 poj2965 The Pilots Brothers' refrigerator
- 【枚举】POJ1753 Flip Game&POJ2965 The Pilots Brothers' refrigerator
- POJ2965_The Pilots Brothers' refrigerator