2013
12-04

# 小希的迷宫

6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0

-1 -1

Yes
Yes
No

#include <cstdio>
#include <cstring>
#define maxn 100005
int fa[maxn];
bool ff[maxn];

int findd( int x )
{
return fa[x] == x ? x : fa[x] = findd(fa[x]);
}
void init()
{
memset(ff, false, sizeof(ff));
for(int i = 1; i < maxn; i++)
fa[i] = i;
}
int main()
{
//freopen("d://in.txt", "r", stdin);
int u,v,x,y,flag = 1;
init();
while(scanf("%d%d",&u,&v) == 2)
{
if(u == -1 && v == -1) break;
else if(u+v == 0)
{
int k = 0;
for(int i = 1; i < maxn; i++)
{
if(ff[i] && fa[i] == i)
k++;
}
if(flag && k <= 1)
puts("Yes");
else
puts("No");
init();
flag = 1;
}
else
{
ff[u] = ff[v] = true;
x = findd(u), y = findd(v);
if(x != y)
fa[x] = y;
else
flag = 0;
}
}
return 0;
}

1. 漂亮。佩服。
P.S. unsigned 应该去掉。换行符是n 不是/n
还可以稍微优化一下，
int main() {
int m,n,ai,aj,bi,bj,ak,bk;
while (scanf("%d%d",&m,&n)!=EOF) {
ai = sqrt(m-1);
bi = sqrt(n-1);
aj = (m-ai*ai-1)>>1;
bj = (n-bi*bi-1)>>1;
ak = ((ai+1)*(ai+1)-m)>>1;
bk = ((bi+1)*(bi+1)-n)>>1;
printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
}
}