题目链接:http://poj.org/problem?id=2585
示例代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
const int maxn = 11, maxm = 1100;
struct Edge {
int v, nxt;
Edge() {};
Edge(int _v, int _nxt) { v = _v; nxt = _nxt; }
} edge[maxm];
int ecnt, head[maxn];
void init() {
ecnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v) {
edge[ecnt] = Edge(v, head[u]);
head[u] = ecnt ++;
}
char s[11];
int a[5][5], in[maxn];
queue<int> que;
bool handle() {
while (!que.empty()) que.pop();
for (int i = 1; i <= 9; i ++) if (!in[i]) que.push(i);
for (int t = 1; t <= 9; t ++) {
if (que.empty()) return false;
int u = que.front();
que.pop();
for (int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
in[v] --;
if (in[v] == 0) que.push(v);
}
}
return true;
}
int main() {
while (~scanf("%s", s) && s[0] != 'E') {
for (int i = 1; i <= 4; i ++)
for (int j = 1; j <= 4; j ++)
scanf("%d", &a[i][j]);
init();
memset(in, 0, sizeof(in));
for (int i = 1; i <= 9; i ++) {
int x = (i-1)/3+1;
int y = (i-1)%3+1;
for (int j = 0; j < 2; j ++) for (int k = 0; k < 2; k ++) {
int num = a[x+j][y+k];
if (num != i) {
addedge(i, num);
in[num] ++;
}
}
}
puts(handle() ? "THESE WINDOWS ARE CLEAN" : "THESE WINDOWS ARE BROKEN");
scanf("%s", s);
}
return 0;
}