zoj1084
#include <stdio.h>
#include <memory.h>
bool g[26][26];
int used[26];
int n;
bool dfs(int id, int color)
{
int i, j;
bool flag;
for (i = 0; i < color; i++)
{
flag = true;
used[id] = i;
for (j = 0; j < id; j++)
if (g[id][j] && used[id] == used[j])
{
flag = false;
break;
}
if (flag && (id == n-1 || dfs(id+1, color)))
return true;
}
return false;
}
int main()
{
int i, j;
bool one;
char adjacent[50];
while (scanf("%d", &n) && n)
{
memset(g, 0, sizeof(g));
memset(used, 0, sizeof(used));
one = true;
for ( i = 0; i < n; i++) {
scanf("%s", adjacent);
for (j = 2; adjacent[j]; j++, one = false)
{
g[i][adjacent[j] - 'A'] = true;
g[adjacent[j] - 'A'][i] = true;
}
}
if (one)
printf("1 channel needed.\n");
else if (dfs(1, 2))
printf("2 channels needed.\n");
else if (dfs(1, 3))
printf("3 channels needed.\n");
else
printf("4 channels needed.\n");
}
return 0;
}
zoj1085
#include <iostream>
#include <queue>
using namespace std;
const int MaxN = 100;
int n, et;
bool used[MaxN];
bool data[MaxN][MaxN];
int dis[MaxN];
void shortest() {
queue<int> q;
q.push(et);
dis[et] = 0;
int x;
while (!q.empty()){
x = q.front();
q.pop();
for (int i = 0; i < n; ++i)
if (data[i][x] && dis[x] + 1 < dis[i]){
q.push(i);
dis[i] = dis[x] + 1;
}
}
}
int search(int id){
if (id == et) return 1;
used[id] = 1;
for (int i = 0; i <n; ++i)
if (!used[i] && data[id][i])
if (search(i)) return 1;
return 0;
}
int main(){
int cases;
int room;
scanf("%d", &cases);
for (int t = 0; t < cases; ++t){
scanf("%d%d\n", &n, &et);
memset(data, 0, sizeof(data));
for (int i = 0; i < MaxN; ++i)
dis[i] = INT_MAX;
char line[10];
int a, b;
while (gets(line)){
if (strcmp(line, "")==0) break;
sscanf(line, "%d%d", &a, &b);
data[a][b] = true;
}
shortest();
int d = dis[0];
room = 0;
for (int i = 1; i < n; ++i){
if (i == et) continue;
memset(used, 0, sizeof(used));
used[i] = 1;
if (!search(0) && dis[i] < d){
room = i;
d = dis[i];
}
}
if (t) cout << endl;
cout << "Put guards in room " << room << "." << endl;
}
return 0;
}
zoj1091
#include <stdio.h>
#include <stdlib.h>
int i, j, m;
void shortested(int k[][64])
{
int x, y;
for(i = 0; i < 64; k[i][i] = 0, ++i)
for(j = 0; j < 64; ++j) {
x = abs(i/8-j/8);
y = abs(i%8-j%8);
if (x==1 && y==2 || x==2 && y==1)
k[i][j] = k[j][i] = 1;
}
for(m = 0; m < 64; ++m)
for(i = 0; i < 64; ++i)
for(j = 0; j < 64; ++j)
if(k[i][m] + k[m][j] < k[i][j])
k[i][j] = k[i][m] + k[m][j];
}
int main()
{
int knight[64][64];
for(i = 0; i < 64; ++i)
for(j = 0; j < 64; ++j)
knight[i][j] = 10;
shortested(knight);
char s[5], t[5];
while (scanf("%s%s", s, t)!=EOF)
{
int x = (s[0]-'a')*8+(s[1]-'1');
int y = (t[0]-'a')*8+(t[1]-'1');
printf("To get from %s to %s ", s, t);
printf("takes %d knight moves.\n", knight[x][y]);
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
struct point{
int x,y;
int c;
}from, to;
int main(){
queue<point> q;
char src[3], dist[3];
int dx[] = {1,1,2,2,-1,-1,-2,-2};
int dy[] = {2,-2,1,-1,2,-2,1,-1};
while(scanf("%s%s", src, dist)!=EOF){
printf("To get from %s to %s ", src, dist);
while(!q.empty()) q.pop();
from.x = src[0]-'a';
from.y = src[1]-'1';
from.c = 0;
to.x = dist[0]-'a';
to.y = dist[1]-'1';
q.push(from);
point temp;
while(true){
from = q.front();
q.pop();
if (from.x==to.x && from.y==to.y) break;
for(int i=0; i<8; i++){
temp.x = from.x + dx[i];
temp.y = from.y + dy[i];
temp.c = from.c + 1;
if(temp.x<0 || temp.x>7 || temp.y<0 || temp.y>7) continue;
q.push(temp);
}
}
printf("takes %d knight moves.\n", from.c);
}
}
#include <stdio.h>
#include <memory.h>
int knight[8][8];
int x[] = {1,1,2,2,-1,-1,-2,-2};
int y[] = {2,-2,1,-1,2,-2,1,-1};
void DFS(int si, int sj, int moves)
{
if(si<0 || sj<0 || si>=8 || sj>=8 || moves>=knight[si][sj]) return;
knight[si][sj] = moves;
int i;
for (i=0; i<8; i++)
DFS(si+x[i], sj+y[i], moves+1);
}
int main()
{
char a[10], b[10];
while(scanf("%s%s", a, b)!=EOF)
{
memset(knight, 10, sizeof(knight));
DFS(a[0]-'a', a[1]-'1', 0);
printf("To get from %s to %s takes %d knight moves.\n",
a, b, knight[b[0]-'a'][b[1]-'1']);
}
return 0;
}
zoj1101
#include <stdio.h>
#include <stdlib.h>
int n,bet[1000],winner;
int comp(const void *a,const void *b)
{
return (*(int *)a - *(int *)b);
}
int bSearch(int k, int val)
{
int left,right,mid;
left = k+1;
right = n-1;
while(left<=right)
{
mid = (left + right)/2;
if(bet[mid] == val) return mid;
else if(bet[mid]<val) left = mid+1;
else right = mid-1;
}
return 0;
}
int solve()
{
int i, j, k;
int minus, pos;
qsort(bet, n, sizeof(int), comp);
for(i=n-1; i>0; i--)
for(j=0; j<i; j++)
for(k=j+1; k<n; k++)
{
minus = bet[i]-bet[j]-bet[k];
pos = bSearch(k, minus);
if(pos && pos!=i)
{
winner = i;
return 1;
}
}
return 0;
}
int main()
{
int i;
while(scanf("%d", &n) && n)
{
for(i=0; i<n; i++)
scanf("%d", &bet[i]);
if(solve()) printf("%d\n", bet[winner]);
else printf("no solution\n");
}
return 0;
}
zoj1103
#include <stdio.h>
#include <memory.h>
#include <ctype.h>
int main()
{
char g[51][51];
unsigned int step[51][51][51];
struct str{
char a,b,c;
}list[51*51*51];
int i, j;
int n;
int a,b,c;
int flag;
while(scanf("%d", &n) && n)
{
scanf("%d%d%d", &a, &b, &c);
memset(step,255,sizeof(step));
unsigned int h = 0;
unsigned int t = 0;
list[0].a = a;
list[0].b = b;
list[0].c = c;
step[a][b][c] = 0;
char arrow;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
while (scanf("%c", &arrow) && !isalpha(arrow));
g[i][j]=arrow;
}
flag = 0;
while(h<=t)
{
a = list[h].a;
b = list[h].b;
c = list[h].c;
if ((a==b)&&(a==c)) {flag = 1; break;}
unsigned int current = step[a][b][c];
current++;
char bcColour = g[b][c];
char *colour=g[a];
colour++;
for(i=1; i<=n; i++,colour++)
if(i!=a && *colour==bcColour && step[i][b][c]>current)
{
step[i][b][c]=current;
t++;
list[t].a=i;
list[t].b=b;
list[t].c=c;
}
bcColour = g[a][c];
colour=g[b],colour++;
for(i=1; i<=n; i++,colour++)
if(i!=b && *colour==bcColour && step[a][i][c]>current)
{
step[a][i][c]=current;
t++;
list[t].a=a;
list[t].b=i;
list[t].c=c;
}
bcColour = g[a][b];
colour=g[c],colour++;
for(i=1; i<=n; i++,colour++)
if(i!=c && *colour==bcColour && step[a][b][i]>current)
{
step[a][b][i]=current;
t++;
list[t].a = a;
list[t].b = b;
list[t].c = i;
}
h++;
}
if (flag) printf("%u\n", step[a][b][c]);
else printf("impossible\n");
}
return 0;
}
zoj1129
#include<iostream>
#include<vector>
#include<map>
using namespace std;
struct {
int who;
int eNum;
}Q[10000];
vector<int> data[10000];
map<string,int> name;
map<string,int>::iterator pos;
int author[10000];
void bfs(string Erdos)
{
memset(author,-1,sizeof(author));
memset(Q,0,sizeof(Q));
Q[0].who = name[Erdos];
Q[0].eNum = 0;
author[name[Erdos]] = 0;
int head = 0;
int tail = 1;
int nowAuthor, many;
while(head!=tail)
{
nowAuthor = Q[head].who;
many = data[nowAuthor].size();
for(int i = 0; i<many; i++)
if(author[data[nowAuthor][i]]==-1)
{
author[data[nowAuthor][i]] = Q[head].eNum+1;
Q[tail].who = data[nowAuthor][i];
Q[tail++].eNum = Q[head].eNum+1;
}
head++;
}
}
int main()
{
int p,n;
int len;
bool title;
int numAuthor, numPaper;
int paper[300];
int iCase = 0;
char str[300];
string fullName;
int i,j;
while(scanf("%d%d",&p,&n) && (p||n)){
for(i = 0; i<10000; i++)
data[i].clear();
name.clear();
numAuthor = 0;
while(p--)
{
title = false;
numPaper = 0;
while(!title)
{
scanf("%s",str);
len = strlen(str);
str[len-1]=' ';
fullName = str;
scanf("%s",str);
len = strlen(str);
if(str[len-1]==':') title = true;
if(str[len-1]=='.') title = true;
str[len-1]=0;
fullName += str;
pos = name.find(fullName);
if(pos==name.end()) name[fullName] = numAuthor++;
paper[numPaper++] = name[fullName];
if(title) gets(str);
}
for(i = 0; i<numPaper; i++)
for(j = 0; j<numPaper; j++)
if(i!=j) data[paper[i]].push_back(paper[j]);
}
string Erdos = "Erdos P.";
pos = name.find(Erdos);
if(pos==name.end()) name[Erdos]=numAuthor++;
bfs(Erdos);
printf("Database #%d\n",++iCase);
while(n--)
{
scanf("%s",str);
printf("%s ",str);
len = strlen(str);
str[len-1]=' ';
fullName = str;
scanf("%s",str);
printf("%s: ",str);
fullName += str;
pos = name.find(fullName);
if(pos==name.end()) printf("infinity\n");
else if(author[name[fullName]]==-1) printf("infinity\n");
else printf("%d\n",author[name[fullName]]);
}
printf("\n");
}
return 0;
}
zoj1136
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
int cmp(const void * a,const void * b)
{
return (*(int *)a - *(int *)b);
}
int n, m;
int a[11];
void bfs()
{
int i, j, k;
int x;
int num;
int y[11];
char b[5001];
int q[5001][3];
q[0][0] = q[0][1] = q[0][2] = 0;
int head = 0;
int tail = 0;
memset(b, 1, sizeof(b));
do
{
for (i=0; i<m; i++)
{
if (head == 0 && a[i] == 0) continue;
x = (q[head][0] * 10 + a[i]) % n;
if (b[x])
{
tail++;
q[tail][0] = x;
q[tail][1] = a[i];
q[tail][2] = head;
b[x] = 0;
if (x==0)
{
num = 0;
k = tail;
while (1)
{
num++;
y[num] = q[k][1];
k = q[k][2];
if (k == 0) break;
}
for (j = num; j >= 1; j--)
printf("%d", y[j]);
printf("\n");
return;
}
}
}
head++;
} while (head <= tail);
printf("0\n");
}
int main()
{
int i;
while(scanf("%d %d", &n, &m) != EOF)
{
for (i = 0; i < m; i++)
scanf("%d", &a[i]);
qsort(a, m, sizeof(int), cmp);
if (n == 0) printf("0\n");
else bfs();
}
return 0;
}
zoj1142
#include<stdio.h>
#include<string.h>
#define N 160
char wall[N*N][4];
char used[N*N];
int next[N*N][4];
int path[N*N];
int n;
int h, w;
void side(char maze[][N]) {
memset(wall,0,sizeof(wall));
int k = 0;
int i, j;
for(i = 0; i<2*h-1; i++)
for(j = 0; j<w; j++,k++)
{
if(i%2==0 && j==w-1) break;
if(i==0)
{
if (maze[0][j]=='/') wall[k][3] = 1;
if (maze[0][j+1]!='/') wall[k][2] = 1;
next[k][3] = w-1+j;
next[k][2] = w+j;
}
else if(i==2*h-2)
{
if (maze[h-1][j]!='/') wall[k][0] = 1;
if (maze[h-1][j+1]=='/') wall[k][1] = 1;
next[k][0] = n-(w+w-1)+j;
next[k][1] = n-(w+w-1)+j+1;
}
else
{
if(i%2==0)
{
if (maze[i/2][j]=='/') wall[k][3] = 1;
else wall[k][0] = 1;
if (maze[i/2][j+1]!='/') wall[k][2] = 1;
else wall[k][1] = 1;
next[k][0] = k-w;
next[k][1] = k-w+1;
next[k][2] = k+w;
next[k][3] = k+w-1;
}
else
{
if (maze[i/2][j]=='/') wall[k][1] = 1;
else wall[k][0]=1;
if (maze[i/2+1][j]=='/') wall[k][3] = 1;
else wall[k][2]=1;
next[k][0] = k-w;
next[k][1] = k-w+1;
next[k][2] = k+w;
next[k][3] = k+w-1;
}
}
if (j==0 && i%2) wall[k][0] = wall[k][3] = 0;
if (i%2 && j==w-1)
wall[k][2] = wall[k][1] = 0;
}
}
char dfs(int i,int length,int start)
{
path[i] = length;
if (i==start && length>2) return 1;
if (used[i]) return 0;
used[i]=1;
if (wall[i][0] && dfs(next[i][0],length+1,start)) return 1;
if (wall[i][1] && dfs(next[i][1],length+1,start)) return 1;
if (wall[i][2] && dfs(next[i][2],length+1,start)) return 1;
if (wall[i][3] && dfs(next[i][3],length+1,start)) return 1;
return 0;
}
int main()
{
int i;
int iCase = 1;
char maze[N][N];
while(scanf("%d %d",&w,&h) && w)
{
for(i=0; i<h; i++)
scanf("%s",maze[i]);
n = (h-1)*(w+w-1)+w-1;
side(maze);
memset(used, 0, sizeof(used));
int total = 0;
int max = 0;
memset(path, 0, sizeof(path));
for(i=0; i<n; i++)
if(!used[i])
{
if(dfs(i,0,i))
{
if (path[i]>max) max = path[i];
total++;
}
used[i]=1;
}
printf("Maze #%d:\n",iCase++);
if(total>0)
printf("%d Cycles; the longest has length %d.\n",total,max);
else
printf("There are no cycles.\n");
printf("\n");
}
return 0;
}
zoj1148
#include <stdio.h>
#include <memory.h>
struct{
int x, y;
} q[3000];
char g[80][80];
int steps[80][80];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int main(){
int i;
int n = 1;
int sx, sy, dx, dy;
int h, w;
while(scanf("%d%d", &w, &h) && (h || w)){
getchar();
int m = 1;
for(i = 1; i <= h; i++)
gets(&g[i][1]);
for(i = 0; i < w + 2; i++)
g[0][i] = g[h + 1][i] = ' ';
for(i = 1; i <= h; i++)
g[i][0] = g[i][w + 1] = ' ';
printf("Board #%d:\n", n++);
while(scanf("%d%d%d%d", &sy, &sx, &dy, &dx)){
if(!(sx || sy || dx|| dy)) break;
memset(steps, -1, sizeof(steps));
g[sx][sy] = g[dx][dy] = ' ';
steps[sx][sy] = 0;
int top = 0;
int tail = 0;
int x0, y0;
q[top].x = sx;
q[top].y = sy;
while(top <= tail && steps[dx][dy] == -1){
for(i = 0; i < 4; i++){
x0 = q[top].x + dir[i][0];
y0 = q[top].y + dir[i][1];
while(x0 >= 0 && x0 <= h+1 && y0 >= 0 && y0 <= w+1
&& g[x0][y0] == ' ' && steps[x0][y0] == -1){
tail++;
q[tail].x = x0;
q[tail].y = y0;
steps[x0][y0] = steps[q[top].x][q[top].y] + 1;
x0 += dir[i][0];
y0 += dir[i][1];
}
}
top++;
}
g[sx][sy] = g[dx][dy] = 'X';
if (steps[dx][dy] == -1)
printf("Pair %d: impossible.\n", m++);
else
printf("Pair %d: %d segments.\n", m++, steps[dx][dy]);
}
printf("\n");
}
return 0;
}