POJ2585 Window Pains
2016-04-10 09:58
375 查看
一.原题链接:http://poj.org/problem?id=2585
二.题目大意:有9个窗口,每个窗口按一定的顺序排列(就是图1中的顺序),每个窗口占2*2个格子,总屏幕有3*3,每个窗口都开着并且可以相互覆盖,给你一个屏幕的状态,让你求出是否可以通过窗口的相互覆盖成为当前屏幕。
图1: 窗口的顺序
三.思路:屏幕中每个格子可以出现的数字如下:
如果屏幕中出现一个数字那么它必定覆盖其他屏幕,比如说在我标示红色那个格子里,如果出现了2,那么2肯定覆盖了3,5,6 。因为窗口都是开着的。所以我们可以通过这一点建图,覆盖指向被覆盖的。比如红色格子如果出现2,就建2->3, 2->5,2->6。然后图建好了之后,进行一次拓扑排序,如果发现有环了,说明矛盾了,不可以有这个屏幕的显示。
四.代码:
二.题目大意:有9个窗口,每个窗口按一定的顺序排列(就是图1中的顺序),每个窗口占2*2个格子,总屏幕有3*3,每个窗口都开着并且可以相互覆盖,给你一个屏幕的状态,让你求出是否可以通过窗口的相互覆盖成为当前屏幕。
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
三.思路:屏幕中每个格子可以出现的数字如下:
1 | 1,2 | 2,3 | 3 |
1,4 | 1,2,4,5 | 2,3,5,6 | 3,6 |
4,7 | 4,5,7,8 | 5,6,8,9 | 6,9 |
7 | 7,8 | 8,9 | 9 |
四.代码:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> #include <vector> using namespace std; const int MAX_SIZE = 10, INF = 0x3f3f3f3f; int screen[4][4], cnt[MAX_SIZE]; int saveCovered[][5] = {{1}, {1, 2}, {2, 3}, {3}, {1, 4}, {1, 2, 4, 5}, {2, 3, 5, 6}, {3, 6}, {4, 7}, {4, 5, 7, 8}, {5, 6, 8, 9}, {6, 9}, {7}, {7, 8}, {8, 9}, {9} }; bool graph[MAX_SIZE][MAX_SIZE]; void buildGraph() { memset(cnt, 0, sizeof(cnt)); memset(graph, 0, sizeof(graph)); int i, j, k; for(i = 0; i < 4; i++) for(j = 0; j < 4; j++) for(k = 0; saveCovered[i*4 + j][k]; k++) if(saveCovered[i*4 + j][k] != screen[i][j]){ if(!graph[screen[i][j]][saveCovered[i*4 + j][k]]){ graph[screen[i][j]][saveCovered[i*4 + j][k]] = true; cnt[saveCovered[i*4 + j][k]]++; } } } bool TopoSort() { int i, j, num, cur; num = 0; for(i = 0; i < 9; i++){ cur = -1; for(j = 1; j <= 9; j++) if(0 == cnt[j]){ cur = j; break; } if(-1 == cur) return false; cnt[cur] = -1; for(j = 0; j < 10; j++) if(graph[cur][j]) cnt[j]--; } return true; } int main() { //freopen("in.txt", "r", stdin); int i, j; char buffer[32]; while(cin>>buffer){ if(buffer[0] == 'E') break; for(i = 0; i < 4; i++) for(j = 0; j < 4; j++) cin>>screen[i][j]; buildGraph(); cin>>buffer; if(TopoSort()) cout<<"THESE WINDOWS ARE CLEAN\n"; else cout<<"THESE WINDOWS ARE BROKEN\n"; } return 0; }
相关文章推荐
- 我与小娜(49):距离人工智能创造音乐已经不远了
- leetcode---Factorial Trailing Zeroes
- beaglebone black xenomai移植 ---fwqlzz love is for ever
- Rails中scope和类方法的区别
- 【转】Condition的await-signal流程详解
- LeetCode *** 219. Contains Duplicate II
- InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效
- InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效
- 阿里云人工智能小Ai是比深度学习更高阶的算法
- http://blog.csdn.net/neiloid/article/details/7037093#
- HDOJ 2057 A + B Again
- kaili aircrack-ng remark
- pair<int, int> set<pair<int, int>>
- gem sources -a https://ruby.taobao.org/ 提示:Error fetching https://ruby.taobao.org/ SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: ce rtificate verify failed
- 2008 APAC local onsites Problem C. Millionaire
- aix 关闭报警黄灯
- 关于[[NSBundle mainBundle] infoDictionary]里面的那些东西
- NFS SERVER 引起aix系统无法启动
- leetcode196-Delete Duplicate Emails(删除重复并且id较大的数据)
- hdu1021 Fibonacci Again