锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鍛紐~~鐖結Y涓?#8230;…
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;
#define UNSURE 0 // 涓嶈兘紜畾
#define CYCLED 1 // 瀛樺湪鍥炶礬
#define SORTED 2 // 宸茬粡鎺掑簭
int n; // n 錛氶《鐐逛釜鏁?#160;
vector<list<int> > g; // g 錛氬浘
vector<int> top; // top 錛氭嫇鎵戝簭鍒?#160;
vector<int> ins; // ins 錛氬叆搴?#160;
int Topsort()
{
bool unsure(false);
queue<int> que;
vector<int> cnt(ins.begin(), ins.end()); // 鐢╟nt瀛樺偍ins錛岄伩鍏嶄慨鏀筰ns銆?#160;
for (int j = 0; j < n; ++j)
if (cnt[j] == 0) que.push(j); // 鍏ュ害涓?鐨勯《鐐瑰叆闃熴?#160;
int u;
list<int>::iterator it;
top.clear();
while (!que.empty())
{
if (que.size() != 1) unsure = true; // 濡傛灉閫夋嫨欏剁偣澶氫綑1涓紝涓嶈兘紜畾銆?#160;
u = que.front(); que.pop();
top.push_back(u);
for (it = g[u].begin(); it != g[u].end(); ++it)
if (--cnt[*it] == 0) que.push(*it);
}
if (top.size() != n) return CYCLED; // 鍏堝垽鏂槸鍚︽湁鐜紝鍗充嬌涓嶈兘紜畾錛?#160;
if (unsure) return UNSURE; // 鍐嶅垽鏂槸鍚︿笉鑳界‘瀹氥?#160;
return SORTED;
}
int main()
{
int steps, ans;
string exp;
int m, u, v, i;
while (cin >> n >> m, n != 0 || m != 0) // 2 <= n <= 26
{
g.assign(n, list<int>());
ins.assign(n, 0);
ans = UNSURE;
for (steps = 0; ans == UNSURE && steps < m; ++steps)
{
cin >> exp;
u = int(exp[0] - 'A'), v = int(exp[2] - 'A');
if (find(g[u].begin(), g[u].end(), v) == g[u].end()) // 濡傛灉涓嶅瓨鍦ㄨ竟(u, v)錛屾坊鍔犮?#160;
g[u].push_back(v), ++ins[v];
ans = Topsort();
}
for (i = steps; i < m; ++i) cin >> exp; // 澶勭悊鍓╀笅鏃犵敤鐨勮緭鍏ユ暟鎹?#160;
if (ans == UNSURE) // Output()
cout << "Sorted sequence cannot be determined." << endl;
else if (ans == CYCLED)
cout << "Inconsistency found after " << steps << " relations." << endl;
else
{
cout << "Sorted sequence determined after " << steps << " relations: ";
for (i = 0; i < n; i++) cout << char('A' + top[i]);
cout << "." << endl;
} // end Output()
}
system("pause");
return 0;
}
ps.絎竴錛岃浠旂粏鐪嬮錛岀浜岋紝榪樻槸瑕佷粩緇嗙湅棰樸?img src ="http://www.shnenglu.com/lzmagic/aggbug/79730.html" width = "1" height = "1" />
]]>
* (1)姹傚悇鐐瑰埌婧愮偣鐨勬渶灝忔鏁幫紙BFS錛?br> * (2)姹傚悇鐐瑰埌緇堢偣鐨勬渶灝忔鏁幫紙BFS錛?br> * (3)濡傛灉鐐逛笉鏄粰瀹氳礬寰勪笂鐨勭偣錛岄偅涔堬細璇ョ偣鍒版簮鐐圭殑鏈灝忔鏁幫紜璇ョ偣鍒扮粓鐐圭殑鏈灝忔鏁?lt;緇欏畾璺緞鐨勬鏁幫紝鍚﹀垯緇欏畾璺緞涓嶆槸鍞竴鏈鐭殑
* (4)濡傛灉涓ょ浉閭葷偣a銆乥涔嬮棿瀛樺湪澧欙紝閭d箞錛歛鍒版簮鐐圭殑鏈灝忔鏁幫紜1錛媌鍒扮粓鐐圭殑鏈灝忔鏁?lt;=緇欏畾璺緞鐨勬鏁?br> * 鎴栬?nbsp;a鍒扮粓鐐圭殑鏈灝忔鏁幫紜1錛媌鍒版簮鐐圭殑鏈灝忔鏁?lt;=緇欏畾璺緞鐨勬鏁幫紝鍚﹀垯澧欏浣?br> * (5)濡傛灉瀛樺湪鐐逛笉鍙揪錛岃鏄庡瓨鍦ㄥ灝嗚鐐瑰皝闂搗鏉ワ紝鍙互璇佹槑澧欒嚦灝戞湁涓鍧楀浣?br> */
#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct Grid
{
bool inpath; // 鏄惁鏄礬寰勬柟鏍?/span>
bool uwal; // 鏄惁鏈変笂澧?/span>
bool rwal; // 鏄惁鏈夊彸澧?/span>
int scnt; // 鍒版簮鐐規鏁?/span>
int dcnt; // 鍒扮粓鐐規鏁?/span>
};
int main(int argc, char** argv)
{
bool ok;
int w, h, cnt, steps; // 1 <= w, h <= 100
string path;
Grid grid[100][100];
queue<pair<int, int> > q;
int t, x, y, desx, desy, x2, y2, i;
for (cin >> t; t > 0; --t)
{
// 鍒濆鍖栨暟鎹?/span>
cin >> w >> h;
for (y = 0; y < h; ++y)
for (x = 0; x < w; ++x)
{
grid[y][x].inpath = false;
grid[y][x].uwal = false;
grid[y][x].rwal = false;
grid[y][x].scnt = -1;
grid[y][x].dcnt = -1;
}
cin >> path;
x = 0, y = 0;
grid[0][0].inpath = true;
steps = path.size();
for (i = 0; i < steps; ++i)
{
switch(path[i])
{
case 'U': ++y; break;
case 'D': --y; break;
case 'L': --x; break;
case 'R': ++x; break;
}
grid[y][x].inpath = true;
}
desx = x, desy = y;
cin >> cnt;
for (i = 0; i < cnt; ++i)
{
cin >> x >> y >> x2 >> y2;
if (x == x2)
if (y + 1 == y2) grid[y][x].uwal = true;
else grid[y2][x].uwal = true;
else
if (x + 1 == x2) grid[y][x].rwal = true;
else grid[y][x2].rwal = true;
}
// 姹傚悇鐐瑰埌婧愮偣鐨勬渶灝忔鏁幫紙BFS錛?/span>
q.push(make_pair(0, 0));
grid[0][0].scnt = 0;
while (!q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1][x].scnt == -1)
{
grid[y + 1][x].scnt = grid[y][x].scnt + 1;
q.push(make_pair(y + 1, x));
}
if (0 < y && grid[y - 1][x].uwal == false && grid[y - 1][x].scnt == -1)
{
grid[y - 1][x].scnt = grid[y][x].scnt + 1;
q.push(make_pair(y - 1, x));
}
if (0 < x && grid[y][x - 1].rwal == false && grid[y][x - 1].scnt == -1)
{
grid[y][x - 1].scnt = grid[y][x].scnt + 1;
q.push(make_pair(y, x - 1));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1].scnt == -1)
{
grid[y][x + 1].scnt = grid[y][x].scnt + 1;
q.push(make_pair(y, x + 1));
}
q.pop();
}
// 姹傚悇鐐瑰埌緇堢偣鐨勬渶灝忔鏁幫紙BFS錛?/span>
q.push(make_pair(desy, desx));
grid[desy][desx].dcnt = 0;
while (!q.empty())
{
y = q.front().first, x = q.front().second;
if (y < h - 1 && grid[y][x].uwal == false && grid[y + 1][x].dcnt == -1)
{
grid[y + 1][x].dcnt = grid[y][x].dcnt + 1;
q.push(make_pair(y + 1, x));
}
if (0 < y && grid[y - 1][x].uwal == false && grid[y - 1][x].dcnt == -1)
{
grid[y - 1][x].dcnt = grid[y][x].dcnt + 1;
q.push(make_pair(y - 1, x));
}
if (0 < x && grid[y][x - 1].rwal == false && grid[y][x - 1].dcnt == -1)
{
grid[y][x - 1].dcnt = grid[y][x].dcnt + 1;
q.push(make_pair(y, x - 1));
}
if (x < w - 1 && grid[y][x].rwal == false && grid[y][x + 1].dcnt == -1)
{
grid[y][x + 1].dcnt = grid[y][x].dcnt + 1;
q.push(make_pair(y, x + 1));
}
q.pop();
}
// 鍒ゆ柇璺緞鏄惁鍞竴鏈鐭紝浠ュ強澧欐槸鍚﹀浣?/span>
ok = true;
for (y = 0; y < h && ok; ++y)
for (x = 0; x < w && ok; ++x)
{
if (grid[y][x].scnt == -1 || grid[y][x].dcnt == -1)
ok = false; // 鏄惁鏈夊皝闂尯鍩?/span>
if (y < h - 1 && grid[y][x].uwal
&& grid[y][x].scnt + grid[y + 1][x].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y + 1][x].scnt + 1 > steps)
ok = false; // 鏄惁涓婂澶氫綑
if (x < w - 1 && grid[y][x].rwal
&& grid[y][x].scnt + grid[y][x + 1].dcnt + 1 > steps
&& grid[y][x].dcnt + grid[y][x + 1].scnt + 1 > steps)
ok = false; // 鏄惁鍙沖澶氫綑
if (!grid[y][x].inpath && grid[y][x].scnt + grid[y][x].dcnt <= steps)
ok = false; // 鏄惁瀛樺湪鏇寸煭璺緞鎴栧彟涓鏈鐭礬寰?/span>
}
if(ok) cout << "CORRECT" << endl;
else cout << "INCORRECT" << endl;
}
return 0;
}
]]>
* FloodFill綆楁硶錛屾繁搴﹂亶鍘嗘悳绱€?br> */
#include <iostream>
#include <map>
using namespace std;
struct Cube
{
bool used; // 鏄惁宸茬粡浣跨敤
int cood[4]; // 鍧愭爣
int neighbor[4][2]; // 鐩擱偦鏍囪瘑鍙?/span>
};
void FloodFill(int row, int cnt, Cube *cube, int maxmin[4][2])
{
if (cube[row].used == false)
{
cube[row].used = true;
int i, j, k, row2;
for (i = 0; i < 4; ++i)
for (j = 0; j < 2; ++j)
if (cube[row].neighbor[i][j] != -1 && cube[cube[row].neighbor[i][j]].used == false)
{
row2 = cube[row].neighbor[i][j];
for (k = 0; k < 4; ++k)
cube[row2].cood[k] = cube[row].cood[k];
if (j == 0)
{
++cube[row2].cood[i];
if (maxmin[i][0] < cube[row2].cood[i])
maxmin[i][0] = cube[row2].cood[i];
}
else
{
--cube[row2].cood[i];
if (maxmin[i][1] > cube[row2].cood[i])
maxmin[i][1] = cube[row2].cood[i];
}
FloodFill(row2, cnt, cube, maxmin);
}
}
}
int main(int argc, char** argv) {
bool ok;
int cnt; // 1 <= cnt <= 100
int maxmin[4][2];
int minv;
Cube cube[100];
map <int, int> idmap;
int t, i, j, k, id;
for (cin >> t; t > 0; --t)
{
// 杈撳叆鏁版嵁
cin >> cnt;
idmap.clear();
for (i = 0; i < cnt; ++i)
{
cube[i].used = false;
cin >> id;
idmap[id] = i;
for (j = 0; j < 4; ++j)
for (k = 0; k < 2; ++k)
cin >> cube[i].neighbor[j][k];
}
// 鏍囪瘑鍙鋒敼涓哄搴旂殑琛屽彿
for (i = 0; i < cnt; ++i)
for (j = 0; j < 4; ++j)
for (k = 0; k < 2; ++k)
cube[i].neighbor[j][k] = (cube[i].neighbor[j][k] == 0) ? -1 : idmap[cube[i].neighbor[j][k]];
// 鍒ゆ柇鏄惁瀵圭О
ok = true;
for (i = 0; i < cnt && ok; ++i)
for (j = 0; j < 4 && ok; ++j)
for (k = 0; k < 2 && ok; ++k)
if (cube[i].neighbor[j][k] != -1 && cube[cube[i].neighbor[j][k]].neighbor[j][1 - k] != i)
ok = false;
if (!ok)
{
cout << "Inconsistent" << endl;
continue;
}
// Flood Fill 綆楁硶 (縐嶅瓙鏌撹壊娉?
for (i = 0; i < 4; ++i) cube[i].cood[i] = 0;
for (i = 0; i < 4; ++i) maxmin[i][0] = maxmin[i][1] = 0;
FloodFill(0, cnt, cube, maxmin);
// 鍒ゆ柇鏄惁榪為?/span>
ok = true;
for (i = 0; i < cnt && ok; ++i)
if (cube[i].used == false)
ok = false;
if (!ok)
{
cout << "Inconsistent" << endl;
continue;
}
// 璁$畻鏈灝忎綋縐?/span>
minv = 1;
for (i = 0; i < 4; ++i)
minv *= maxmin[i][0] - maxmin[i][1] + 1;
cout << minv << endl;
}
return 0;
}
嫻嬭瘯鏁版嵁錛?br>Input:
6
9
1 2 3 4 5 6 7 8 9
2 0 1 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0
5 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0
7 0 0 0 0 1 0 0 0
8 0 0 0 0 0 0 0 1
9 0 0 0 0 0 0 1 0
2
3 0 0 1 0 0 0 0 0
1 0 0 3 0 0 0 0 0
4
1 2 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0
3 0 0 4 0 0 0 0 0
4 0 0 0 3 0 0 0 0
5
101 2 0 0 0 0 0 0 0
2 0 101 321 0 0 0 0 0
321 4 0 0 2 0 0 0 0
4 5 321 0 0 0 0 0 0
5 0 4 0 0 0 0 0 0
1
10 0 0 0 0 0 0 0 0
4
1 0 2 4 0 0 0 0 0
2 1 0 3 0 0 0 0 0
3 4 0 0 2 0 0 0 0
4 0 3 0 1 0 0 0 0
Output:
81
Inconsistent
Inconsistent
8
1
4
]]>