uva321 The New Villa

2024-06-12 17:58
文章标签 new villa uva321

本文主要是介绍uva321 The New Villa,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

好久好久没一次ac了。。。。

隐式图搜索,直接可以用数组记录是否访问,bfs中保存每个状态之前的状态下标,便于打印路径。

依然出现了一些不该出现的错误,还是要加强基本功。


#include<cstdio>
#include<cstring>
#include<algorithm>
#define HASHSIZE 110000
#define MAX 1000000
#define ROOM 11
using namespace std;int R,D,S,amount[MAX],father[MAX];
char door[ROOM][ROOM],onoff[ROOM][ROOM],s[MAX][ROOM+1],vis[HASHSIZE];int insert(int cur)
{int value=s[cur][0]*10000,i;for(i=1;i<=R;i++){value+=s[cur][i]*(1<<(i-1));}if(vis[value]==1)return 0;else{vis[value]=1;return 1;}
}void print(int cur)
{int i,j=father[cur];//printf("%d %d\n",cur,j);if(j==-1)return ;print(j);if(s[cur][0]!=s[j][0]){printf("- Move to room %d.\n",s[cur][0]);}for(i=1;i<=R;i++){if(s[cur][i]!=s[j][i]){if(s[cur][i]==1)printf("- Switch on light in room %d.\n",i);elseprintf("- Switch off light in room %d.\n",i);//写错了break;}}
}
void bfs()
{int front=0,rear=1,i,j,flag=0,now;memset(s[0],0,ROOM+1);s[front][0]=1,s[front][1]=1,amount[front]=0,father[front]=-1;memset(vis,0,HASHSIZE);while(front<rear){if(s[front][0]==R){for(i=1;i<=R;i++)if(s[front][i]==1)break;if(i==R){flag=1;break;}}now=s[front][0];for(i=1;i<=R;i++)//改变房间{if(door[now][i]==1&&s[front][i]==1){memcpy(s[rear],s[front],ROOM+1);s[rear][0]=i;if(insert(rear)==1){father[rear]=front;amount[rear]=amount[front]+1;rear++;}}}for(i=1;i<=R;i++)//开关灯{if(i!=now&&onoff[now][i]==1){memcpy(s[rear],s[front],ROOM+1);if(s[rear][i]==0)s[rear][i]=1;elses[rear][i]=0;if(insert(rear)==1){father[rear]=front;amount[rear]=amount[front]+1;rear++;}}}front++;}if(flag==0){printf("The problem cannot be solved.\n");}else{printf("The problem can be solved in %d steps:\n",amount[front]);print(front);}
}int main()
{int i,j,a,b,all=1;while(scanf("%d%d%d",&R,&D,&S)&&R+D+S){memset(door,0,ROOM*ROOM);memset(onoff,0,ROOM*ROOM);for(i=0;i<D;i++){scanf("%d%d",&a,&b);door[a][b]=door[b][a]=1;}for(i=0;i<S;i++){scanf("%d%d",&a,&b);onoff[a][b]=1;}printf("Villa #%d\n",all++);bfs();printf("\n");}return 0;
}



这篇关于uva321 The New Villa的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054925

相关文章

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

Python中__new__()方法适应及注意事项详解

《Python中__new__()方法适应及注意事项详解》:本文主要介绍Python中__new__()方法适应及注意事项的相关资料,new()方法是Python中的一个特殊构造方法,用于在创建对... 目录前言基本用法返回值单例模式自定义对象创建注意事项总结前言new() 方法在 python 中是一个

Golan中 new() 、 make() 和简短声明符的区别和使用

《Golan中new()、make()和简短声明符的区别和使用》Go语言中的new()、make()和简短声明符的区别和使用,new()用于分配内存并返回指针,make()用于初始化切片、映射... 详细介绍golang的new() 、 make() 和简短声明符的区别和使用。文章目录 `new()`

java线程深度解析(一)——java new 接口?匿名内部类给你答案

http://blog.csdn.net/daybreak1209/article/details/51305477 一、内部类 1、内部类初识 一般,一个类里主要包含类的方法和属性,但在Java中还提出在类中继续定义类(内部类)的概念。 内部类的定义:类的内部定义类 先来看一个实例 [html]  view plain copy pu

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

List list = new ArrayList();和ArrayList list=new ArrayList();的区别?

List是一个接口,而ArrayList 是一个类。 ArrayList 继承并实现了List。 List list = new ArrayList();这句创建了一个ArrayList的对象后把上溯到了List。此时它是一个List对象了,有些ArrayList有但是List没有的属性和方法,它就不能再用了。而ArrayList list=new ArrayList();创建一对象则保留了A

vue原理分析(六)--研究new Vue()

今天我们来分析使用new Vue() 之前研究时,只是说是在创建一个实例。并没有深入进行研究 在vue的源码中找下Vue的构造函数 function Vue(options) {if (!(this instanceof Vue)) {warn$2('Vue is a constructor and should be called with the `new` keyword');}thi

GTK中创建线程函数g_thread_new和g_thread_create的区别

使用GThread函数,需要引用glib.h头文件。 这两个接口的核心区别就是  g_thread_create 是旧的接口,现在已经不使用了,而g_thread_new是新的接口,建议使用。 g_thread_create: g_thread_create has been deprecated since version 2.32 and should not be used in n

New的VC编译器实现

当我们调用 new 的时候,例如 int *p = new int; 时,编译器到底作了什么工作呢?跟进断点看一看。   (在 vc debug模式下 ) double *p1 = new double ; 00411A6E  push        8    00411A70  call        operator new (4111B8h) 00411A75  add

Python方法:__init__,__new__,__class__的使用详解

转自:https://blog.csdn.net/qq_26442553/article/details/82464682 因为python中所有类默认继承object类。而object类提供了了很多原始的内建属性和方法,所以用户自定义的类在Python中也会继承这些内建属性。可以使用dir()函数可以查看,虽然python提供了很多内建属性但实际开发中常用的不多。而很多系统提供的内建属性实际