UVA 321 - The New Villa
来源:互联网 发布:df什么意思网络用语 编辑:程序博客网 时间:2024/06/05 15:38
/* bfs找最短路,hash判重 二进制的状态压缩
*/
#include<cstdio>
#include<cstring>#include<vector>
#include<cstdlib>
using namespace std;
vector<int> G[20];
vector<int> sw[20];
int r,d,s;
int vis[2000000];
struct node
{
int r,step,light,fr;
} p[200000];
void init()
{
memset(p,0,sizeof(p));
memset(vis,0,sizeof(vis));
for(int i = 0; i < 20; i++)
{
G[i].clear();
sw[i].clear();
}
}
int insert(int x)
{
int temp = p[x].light*10 + p[x].r-1;
if(vis[temp])
return 0;
return vis[temp]=1;
}
int cal(int x,int y)
{
x = abs(x-y);
for(int i = 0; i < 20; i++)
{
if(x==(1<<i)) return i+1;
}
}
int print(int u)
{
int v = p[u].fr;
if(v)
print(v);
if(p[v].r==p[u].r&&p[u].light<p[v].light)
printf("- Switch off light in room %d.\n",cal(p[u].light,p[v].light));
else if(p[v].r==p[u].r&&p[u].light>p[v].light)
printf("- Switch on light in room %d.\n",cal(p[u].light,p[v].light));
else if(p[v].r!=p[u].r)
printf("- Move to room %d.\n",p[u].r);
}
int bfs()
{
int fronts = 0,rears=1;
p[fronts].fr=0;
p[fronts].light=1;
p[fronts].r=1;
p[fronts].step=0;
insert(0);
while(fronts<rears)
{
int x = p[fronts].r;
if(x==r&&p[fronts].light==1<<(x-1))
{
printf("The problem can be solved in %d steps:\n",p[fronts].step);
print(fronts);
return 0;
}
for(int i = 0; i < G[x].size(); i++)
{
if((1<<(G[x][i]-1))&p[fronts].light)
{
p[rears].fr=fronts;
p[rears].light = p[fronts].light;
p[rears].step = p[fronts].step+1;
p[rears].r = G[x][i];
if(insert(rears))
{
rears++;
}
}
}
for(int i = 0; i < sw[x].size(); i++)
{
p[rears].fr=fronts;
p[rears].step = p[fronts].step + 1;
p[rears].r = p[fronts].r;
if((1<<(sw[x][i]-1))&p[fronts].light)
p[rears].light = p[fronts].light - (1<<(sw[x][i]-1));
else
p[rears].light = p[fronts].light + (1<<(sw[x][i]-1));
if((((1<<x-1))&p[rears].light)&&insert(rears))
{
rears++;
}
}
fronts++;
}
printf("The problem cannot be solved.\n");
}
int main()
{
int a,b,kc=0;
while(scanf("%d %d %d",&r,&d,&s)==3)
{
if(!r&&!d&&!s) break;
init();
//printf("%d %d %d\n",r,d,s);
for(int i = 0; i < d; i++)
{
scanf("%d %d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 0; i < s; i++)
{
scanf("%d %d",&a,&b);
sw[a].push_back(b);
}
printf("Villa #%d\n",++kc);
bfs();
printf("\n");
}
return 0;
}
- uva 321 - The New Villa
- UVa 321 - The New Villa
- UVA 321 - The New Villa
- uva 321 - The New Villa
- uva 321 The New Villa
- UVa 321 - The New Villa
- UVA - 321 The New Villa
- uva 321 - The New Villa bfs+递归
- uva:321 - The New Villa(bfs + 哈希判重)
- UVa 321 & ZOJ 1301 - The New Villa
- UVA - 321 The New Villa 隐式图搜索
- The New Villa (Uva 321 bfs)
- 321 - The New Villa
- UVA 321 - The New Villa BFS+位运算
- UVA 321 - The New Villa
- uva 321 The New Villa(隐式图搜索)
- UVA - 321 The New Villa(bfs+hash)
- uvaoj 321 - The New Villa
- 统计一个字符串中出现的字符及其次数
- test_openssl
- 704 - Colour Hash
- 一个TCP包中的数据数据问题
- 线程高级应用-心得1-传统线程和定时器讲解及案例分析
- UVA 321 - The New Villa
- UVA 165- Stamps
- 腾讯笔试面试题
- 线程高级应用-心得2-同步锁讲解及面试题案例分析
- UVA 208 - Firetruck
- 常用字符串分割(C++)
- UVA301- Transportation
- Android ListView getView方法 性能优化【转】
- 线程高级应用-心得3-线程范围内的共享变量以及应用场景及面试题案例分析