hdu4770Lights Against Dudely
2013-11-21 19:30
323 查看
Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 680 Accepted Submission(s): 204
[align=left]Problem Description[/align]
Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money."
Hagrid: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. Except perhaps Hogwarts."
— Rubeus Hagrid to Harry Potter.
Gringotts Wizarding Bank is the only bank of the wizarding world, and is owned and operated by goblins. It was created by a goblin called Gringott. Its main offices are located in the North Side of Diagon Alley in London, England. In addition to storing money and valuables for wizards and witches, one can go there to exchange Muggle money for wizarding money. The currency exchanged by Muggles is later returned to circulation in the Muggle world by goblins. According to Rubeus Hagrid, other than Hogwarts School of Witchcraft and Wizardry, Gringotts is the safest place in the wizarding world.
The text above is quoted from Harry Potter Wiki. But now Gringotts Wizarding Bank is not safe anymore. The stupid Dudley, Harry Potter's cousin, just robbed the bank. Of course, uncle Vernon, the drill seller, is behind the curtain because he has the most advanced drills in the world. Dudley drove an invisible and soundless drilling machine into the bank, and stole all Harry Potter's wizarding money and Muggle money. Dumbledore couldn't stand with it. He ordered to put some magic lights in the bank rooms to detect Dudley's drilling machine. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:
Some rooms are indestructible and some rooms are vulnerable. Dudely's machine can only pass the vulnerable rooms. So lights must be put to light up all vulnerable rooms. There are at most fifteen vulnerable rooms in the bank. You can at most put one light in one room. The light of the lights can penetrate the walls. If you put a light in room (x,y), it lights up three rooms: room (x,y), room (x-1,y) and room (x,y+1). Dumbledore has only one special light whose lighting direction can be turned by 0 degree,90 degrees, 180 degrees or 270 degrees. For example, if the special light is put in room (x,y) and its lighting direction is turned by 90 degrees, it will light up room (x,y), room (x,y+1 ) and room (x+1,y). Now please help Dumbledore to figure out at least how many lights he has to use to light up all vulnerable rooms.
Please pay attention that you can't light up any indestructible rooms, because the goblins there hate light.
[align=left]Input[/align]
There are several test cases.
In each test case:
The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 200).
Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, and '.' means a vulnerable room.
The input ends with N = 0 and M = 0
[align=left]Output[/align]
For each test case, print the minimum number of lights which Dumbledore needs to put.
If there are no vulnerable rooms, print 0.
If Dumbledore has no way to light up all vulnerable rooms, print -1.
[align=left]Sample Input[/align]
2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
0 0
[align=left]Sample Output[/align]
0
2
-1
[align=left]Source[/align]
2013 Asia Hangzhou Regional Contest
枚举所有情况( at most fifteen 差不多就是暗示我们枚举吧)
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<vector> #include<queue> using namespace std; #define maxn 210 #define LL long long #define mod 1000000007 int n , m , b[maxn][maxn],k,num; int xx[17] , yy[17] , c[17] , d[17] ; char a[maxn][maxn] ; bool vi[19] ; vector<int>q ; bool check1( int i ) { int x , y , tt ; x = xx[i] ; y = yy[i] ; if((x > 1 && a[x-1][y] == '#')||( y < n && a[x][y+1] == '#')) return false ; tt = b[x][y] ; if(!vi[tt]){ num++; vi[tt] = 1 ;} tt = b[x-1][y] ; if(x > 1 && !vi[tt]){ num++; vi[tt] = 1 ;} tt = b[x][y+1] ; if(!vi[tt] && y < m ){ num++; vi[tt] = 1 ;} return true ; } bool check2( int i ) { int x , y , tt ; x = xx[i] ; y = yy[i] ; if((x < n && a[x+1][y] == '#')||( y < n && a[x][y+1] == '#')) return false ; tt = b[x][y] ; if(!vi[tt]){ num++; vi[tt] = 1 ;} tt = b[x+1][y] ; if(!vi[tt] && x < n ) { num++; vi[tt] = 1 ;} tt = b[x][y+1] ; if(!vi[tt] && y < m ){ num++; vi[tt] = 1 ;} return true ; } bool check3( int i ) { int x , y , tt ; x = xx[i] ; y = yy[i] ; if((x < n && a[x+1][y] == '#')||( y > 1 && a[x][y-1] == '#')) return false ; tt = b[x][y] ; if(!vi[tt]){ num++; vi[tt] = 1 ;} tt = b[x+1][y] ; if(!vi[tt] && x < n ) { num++; vi[tt] = 1 ;} tt = b[x][y-1] ; if(!vi[tt] && y > 1 ){ num++; vi[tt] = 1 ;} return true ; } bool check4( int i ) { int x , y , tt ; x = xx[i] ; y = yy[i] ; if((x > 1 && a[x-1][y] == '#')||( y > 1 && a[x][y-1] == '#')) return false ; tt = b[x][y] ; if(!vi[tt]){ num++; vi[tt] = 1 ;} tt = b[x-1][y] ; if(!vi[tt] && x > 1 ) { num++; vi[tt] = 1 ;} tt = b[x][y-1] ; if(!vi[tt] && y > 1 ){ num++; vi[tt] = 1 ;} return true ; } bool find1() { memset(vi,0,sizeof(vi)) ; int i , j ; num = 0; for( i = 1 ; i <= k ;i++) { if(!c[i])continue ; if(!check1(i)) return false ; } if(num >= k) return true ; return false; } bool find2() { memset(vi,0,sizeof(vi)) ; int i , j ; num = 0; for( i = 1 ; i <= k ;i++) { if(!c[i])continue ; if( c[i] == 1 &&!check1(i))return false ; else if(c[i] == 2 &&!check2(i)) return false ; } if(num >= k) return true ; return false; } bool find3() { memset(vi,0,sizeof(vi)) ; int i , j ; num = 0; for( i = 1 ; i <= k ;i++) { if(!c[i])continue ; if( c[i] == 1 &&!check1(i))return false ; else if(c[i] == 2 &&!check3(i)) return false ; } if(num >= k) return true ; return false; } bool find4() { memset(vi,0,sizeof(vi)) ; int i , j ; num = 0; for( i = 1 ; i <= k ;i++) { if(!c[i])continue ; if( c[i] == 1 &&!check1(i))return false ; else if(c[i] == 2 &&!check4(i)) return false ; } if(num >= k) return true ; return false; } bool getans( int t ) { if(find1())return true;//这是第一个方向 int i , mm , j , jj ; mm = q.size() ; if(mm == 0 ) return false ; jj = q[0] ; c[jj] = 2 ; if(find2()||find3()||find4()) return true ;//枚举其他三个转向 for( i = 1 ; i < mm ;i++ ) { j = q[i] ; c[j] = 2 ; c[jj] = 1 ;//把上一个复位 jj = j ; if(find2()||find3()||find4()) return true ; } return false ; } int main() { int i , j ; int ans , tt , t ; bool flag ; //freopen("in.txt","r",stdin) ; while( scanf("%d%d" , &n ,&m ) != EOF ) { if( n+m == 0 ) break ; for( i = 1 ; i <= n ;i++) scanf("%s" , a[i]+1) ; k = 0 ; memset(b,0,sizeof(b)) ; for( i = 1 ; i <= n ;i++) for( j = 1 ; j <= m ;j++)if(a[i][j] =='.') { xx[++k] = i ; yy[k] = j ; b[i][j] = k ; } if(!k) { puts("0") ; continue ; } int kk = (1<<(k)) ; ans = 9999 ; //利用二进制枚举 for( i = 0 ; i < kk ;i++ ) { memset(c,0,sizeof(c)) ; tt = t = 0 ; q.clear() ; for( j = 1 ; j < kk ; j <<= 1 ) { if(i&j){ t++;c[++tt] = 1 ;q.push_back(tt) ;}//记录为一的地方 else tt++ ; } if(getans(t))ans = min(ans,t) ; } if(ans != 9999) printf("%d\n",ans) ; else puts("-1") ; } return 0 ; }
相关文章推荐
- 我到底需要多少内存(part 1) - 什么是retained heap?
- 生产环境下的mail命令 替代工具,第一款 msmtp
- grails
- editor does not contain a main type" 错误解决方
- editor does not contain a main type" 错误解决方
- 一,问题描述 MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引
- AIX常用命令集锦
- LNK2005 _DllMain@12 mfcs100d.lib
- 关于如GMail等邮箱每天可发送邮件量额度的问题
- AIX密码过期管理
- adb failed to start daemon 的解决办法
- 合法E-mail地址:
- 每日英语:The Toy Mogul Who Became A Billionaire Through His Fight To The Death With Barbie
- rails常用验证方法
- 关于WM_PAINT消息中只重绘无效区的问题
- retain和copy的区别
- centerOS6.4 图形界面组建raid5
- hdu 4308 Saving Princess claire_(BFS)
- Thread_wait、notify、notifyAll的使用方法
- Attribute names cannot contain colons jdom生成带有名字空间的xml元素