-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aoapc-book
committed
Jun 30, 2014
1 parent
9d68027
commit 85b0737
Showing
6 changed files
with
325 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// UVa1103 Ancient Messages | ||
// Rujia Liu | ||
// we pad one empty line/column to the top/bottom/left/right border, so color 1 is always "background" white | ||
#include<cstdio> | ||
#include<cstring> | ||
#include<algorithm> | ||
#include<vector> | ||
#include<set> | ||
using namespace std; | ||
|
||
char bin[256][5]; | ||
|
||
const int maxh = 200 + 5; | ||
const int maxw = 50 * 4 + 5; | ||
|
||
int H, W, pic[maxh][maxw], color[maxh][maxw]; | ||
char line[maxw]; | ||
|
||
void decode(char ch, int row, int col) { | ||
for(int i = 0; i < 4; i++) | ||
pic[row][col+i] = bin[ch][i] - '0'; | ||
} | ||
|
||
const int dr[] = {-1, 1, 0, 0}; | ||
const int dc[] = {0, 0, -1, 1}; | ||
|
||
// dfs from (row, col) and paint color c | ||
void dfs(int row, int col, int c) { | ||
color[row][col] = c; | ||
for(int i = 0; i < 4; i++) { | ||
int row2 = row + dr[i]; | ||
int col2 = col + dc[i]; | ||
if(row2 >= 0 && row2 < H && col2 >= 0 && col2 < W && pic[row2][col2] == pic[row][col] && color[row2][col2] == 0) | ||
dfs(row2, col2, c); | ||
} | ||
} | ||
|
||
vector<set<int> > neighbors; | ||
|
||
void check_neighbors(int row, int col) { | ||
for(int i = 0; i < 4; i++) { | ||
int row2 = row + dr[i]; | ||
int col2 = col + dc[i]; | ||
if(row2 >= 0 && row2 < H && col2 >= 0&& col2 < W && pic[row2][col2] == 0 && color[row2][col2] != 1) | ||
neighbors[color[row][col]].insert(color[row2][col2]); | ||
} | ||
} | ||
|
||
const char* code = "WAKJSD"; | ||
|
||
char recognize(int c) { | ||
int cnt = neighbors[c].size(); | ||
return code[cnt]; | ||
} | ||
|
||
// use this function to print the decoded picture | ||
void print() { | ||
for(int i = 0; i < H; i++) { | ||
for(int j = 0; j < W; j++) printf("%d", pic[i][j]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
int main() { | ||
strcpy(bin['0'], "0000"); | ||
strcpy(bin['1'], "0001"); | ||
strcpy(bin['2'], "0010"); | ||
strcpy(bin['3'], "0011"); | ||
strcpy(bin['4'], "0100"); | ||
strcpy(bin['5'], "0101"); | ||
strcpy(bin['6'], "0110"); | ||
strcpy(bin['7'], "0111"); | ||
strcpy(bin['8'], "1000"); | ||
strcpy(bin['9'], "1001"); | ||
strcpy(bin['a'], "1010"); | ||
strcpy(bin['b'], "1011"); | ||
strcpy(bin['c'], "1100"); | ||
strcpy(bin['d'], "1101"); | ||
strcpy(bin['e'], "1110"); | ||
strcpy(bin['f'], "1111"); | ||
|
||
int kase = 0; | ||
while(scanf("%d%d", &H, &W) == 2 && H) { | ||
memset(pic, 0, sizeof(pic)); | ||
for(int i = 0; i < H; i++) { | ||
scanf("%s", line); | ||
for(int j = 0; j < W; j++) | ||
decode(line[j], i+1, j*4+1); | ||
} | ||
|
||
H += 2; | ||
W = W * 4 + 2; | ||
|
||
int cnt = 0; | ||
vector<int> cc; // connected components of 1 | ||
memset(color, 0, sizeof(color)); | ||
for(int i = 0; i < H; i++) | ||
for(int j = 0; j < W; j++) | ||
if(!color[i][j]) { | ||
dfs(i, j, ++cnt); | ||
if(pic[i][j] == 1) cc.push_back(cnt); | ||
} | ||
|
||
neighbors.clear(); | ||
neighbors.resize(cnt+1); | ||
for(int i = 0; i < H; i++) | ||
for(int j = 0; j < W; j++) | ||
if(pic[i][j] == 1) | ||
check_neighbors(i, j); | ||
|
||
vector<char> ans; | ||
for(int i = 0; i < cc.size(); i++) | ||
ans.push_back(recognize(cc[i])); | ||
sort(ans.begin(), ans.end()); | ||
|
||
printf("Case %d: ", ++kase); | ||
for(int i = 0; i < ans.size(); i++) printf("%c", ans[i]); | ||
printf("\n"); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// UVa1599 Idea Path | ||
// Rujia Liu | ||
#include<cstdio> | ||
#include<cstring> | ||
#include<vector> | ||
#include<queue> | ||
using namespace std; | ||
|
||
const int maxn = 100000 + 5; | ||
const int INF = 1000000000; // maximal color | ||
|
||
struct Edge { | ||
int u, v, c; | ||
Edge(int u=0, int v=0, int c=0):u(u),v(v),c(c) {} | ||
}; | ||
|
||
vector<Edge> edges; | ||
vector<int> G[maxn]; | ||
|
||
void AddEdge(int u, int v, int c) { | ||
edges.push_back(Edge(u, v, c)); | ||
int idx = edges.size() - 1; | ||
G[u].push_back(idx); | ||
} | ||
|
||
int n, vis[maxn]; | ||
int d[maxn]; | ||
// reverse bfs to find out the distance from each node to n-1 | ||
void rev_bfs() { | ||
memset(vis, 0, sizeof(vis)); | ||
d[n-1] = 0; | ||
vis[n-1] = true; | ||
|
||
queue<int> q; | ||
q.push(n-1); | ||
while(!q.empty()) { | ||
int v = q.front(); q.pop(); | ||
for(int i = 0; i < G[v].size(); i++) { | ||
int e = G[v][i]; | ||
int u = edges[e].v; | ||
if(!vis[u]) { | ||
vis[u] = true; | ||
d[u] = d[v] + 1; | ||
q.push(u); | ||
} | ||
} | ||
} | ||
} | ||
|
||
vector<int> ans; | ||
|
||
// forward bfs to construct the path | ||
void bfs() { | ||
memset(vis, 0, sizeof(vis)); | ||
vis[0] = true; | ||
ans.clear(); | ||
|
||
vector<int> next; | ||
next.push_back(0); | ||
for(int i = 0; i < d[0]; i++) { | ||
int min_color = INF; | ||
for(int j = 0; j < next.size(); j++) { | ||
int u = next[j]; | ||
for(int k = 0; k < G[u].size(); k++) { | ||
int e = G[u][k]; | ||
int v = edges[e].v; | ||
if(d[u] == d[v] + 1) | ||
min_color = min(min_color, edges[e].c); | ||
} | ||
} | ||
ans.push_back(min_color); | ||
|
||
// find out the next vertices of the next phase | ||
vector<int> next2; | ||
for(int j = 0; j < next.size(); j++) { | ||
int u = next[j]; | ||
for(int k = 0; k < G[u].size(); k++) { | ||
int e = G[u][k]; | ||
int v = edges[e].v; | ||
if(d[u] == d[v] + 1 && !vis[v] && edges[e].c == min_color) { | ||
vis[v] = true; | ||
next2.push_back(v); | ||
} | ||
} | ||
} | ||
next = next2; | ||
} | ||
|
||
printf("%d\n", ans.size()); | ||
printf("%d", ans[0]); | ||
for(int i = 1; i < ans.size(); i++) printf(" %d", ans[i]); | ||
printf("\n"); | ||
} | ||
|
||
int main() { | ||
int u, v, c, m; | ||
while(scanf("%d%d", &n, &m) == 2) { | ||
edges.size(); | ||
for(int i = 0; i < n; i++) G[i].clear(); | ||
while(m--) { | ||
scanf("%d%d%d", &u, &v, &c); | ||
AddEdge(u-1, v-1, c); | ||
AddEdge(v-1, u-1, c); | ||
} | ||
rev_bfs(); | ||
bfs(); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// UVa1343 The Rotation Game | ||
// Rujia Liu | ||
// This solutions uses IDA* instead of BFS described in the book, because it's shorter 8-) | ||
// It's shorter because no need for lookup tables and "automatically" lexicographically smallest solution. | ||
#include<cstdio> | ||
#include<algorithm> | ||
using namespace std; | ||
|
||
/* | ||
00 01 | ||
02 03 | ||
04 05 06 07 08 09 10 | ||
11 12 | ||
13 14 15 16 17 18 19 | ||
20 21 | ||
22 23 | ||
*/ | ||
|
||
// lines E~H are computed with the help of rev[] | ||
int line[8][7]={ | ||
{ 0, 2, 6,11,15,20,22}, // A | ||
{ 1, 3, 8,12,17,21,23}, // B | ||
{10, 9, 8, 7, 6, 5, 4}, // C | ||
{19,18,17,16,15,14,13}, // D | ||
}; | ||
|
||
const int rev[8] = {5, 4, 7, 6, 1, 0, 3, 2}; // reverse lines of each line | ||
|
||
// center squares | ||
const int center[8] = {6, 7, 8, 11, 12, 15, 16, 17}; | ||
|
||
int a[24]; | ||
char ans[1000]; | ||
|
||
bool is_final() { | ||
for(int i = 0; i < 8; i++) | ||
if (a[center[i]] != a[center[0]]) return false; | ||
return true; | ||
} | ||
|
||
int diff(int target) { | ||
int ans = 0; | ||
for(int i = 0; i < 8; i++) | ||
if(a[center[i]] != target) ans++; | ||
return ans; | ||
} | ||
|
||
inline int h() { | ||
return min(min(diff(1), diff(2)), diff(3)); | ||
} | ||
|
||
inline void move(int i) { | ||
int tmp = a[line[i][0]]; | ||
for(int j = 0; j < 6; j++) a[line[i][j]] = a[line[i][j+1]]; | ||
a[line[i][6]] = tmp; | ||
} | ||
|
||
bool dfs(int d, int maxd) { | ||
if(is_final()) { | ||
ans[d] = '\0'; | ||
printf("%s\n", ans); | ||
return true; | ||
} | ||
if(d + h() > maxd) return false; | ||
for(int i = 0; i < 8; i++) { | ||
ans[d] = 'A' + i; | ||
move(i); | ||
if(dfs(d+1, maxd)) return true; | ||
move(rev[i]); | ||
} | ||
return false; | ||
} | ||
|
||
int main() { | ||
for(int i = 4; i < 8; i++) | ||
for(int j = 0; j < 7; j++) line[i][j] = line[rev[i]][6-j]; | ||
|
||
while(scanf("%d", &a[0]) == 1 && a[0]) { | ||
for(int i = 1; i < 24; i++) scanf("%d", &a[i]); | ||
for(int i = 0; i < 24; i++) if(!a[i]) return 0; | ||
if(is_final()) { | ||
printf("No moves needed\n"); | ||
} else { | ||
for(int maxd = 1; ; maxd++) | ||
if(dfs(0, maxd)) break; | ||
} | ||
printf("%d\n", a[6]); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters