我是zoro
分类: LINUX
2010-11-28 22:47:27
[root@mip-123456 163]# cat 163.c
#include<stdio.h>
#include<stdlib.h>
int find_res(int x,int y)
{
int num = 1;
if((x == 0)&&(y == 0))
return 1;
int flag = 1;
int tmp_x = 0;
int tmp_y = 0;
int r_num = 0;
int l_num = 0;
int u_num = 0;
int d_num = 0;
int i;
enum state{right,left,up,down};
enum state state = right;
while(flag)
{
switch(state)
{
case right:
r_num = 0;
for(i=0;i<l_num+1;i++)
{
r_num++;
tmp_x++;
num++;
// printf("right num is %d\n",num);
if((tmp_x == x)&&(tmp_y == y))
{
flag = 0;
break;
}
}
state = down;
break;
case down:
d_num = 0;
for(i=0;i<u_num+1;i++)
{
d_num++;
tmp_y++;
num++;
// printf("down num is %d\n",num);
if((tmp_x == x)&&(tmp_y == y))
{
flag = 0;
break;
}
}
state = left;
break;
case left:
l_num = 0;
for(i=0;i<r_num+1;i++)
{
l_num++;
tmp_x--;
num++;
// printf("left num is %d\n",num);
if((tmp_x == x)&&(tmp_y == y))
{
flag = 0;
break;
}
}
state = up;
break;
case up:
u_num = 0;
for(i=0;i<d_num+1;i++)
{
u_num++;
tmp_y--;
num++;
// printf("up num is %d\n",num);
if((tmp_x == x)&&(tmp_y == y))
{
flag = 0;
break;
}
}
state = right;
break;
default:
break;
}
}
return num;
}
int main(int argc,char* argv[])
{
int i = 0;
int j = 0;
for(i=-1;i<=2;i++)
{
for(j = -1;j<=2;j++)
{
printf("(%d,%d) is %d\n",i,j,find_res(i,j));
}
}
return 0;
}
[root@mip-123456 163]# ./163
(-1,-1) is 7
(-1,0) is 6
(-1,1) is 5
(-1,2) is 16
(0,-1) is 8
(0,0) is 1
(0,1) is 4
(0,2) is 15
(1,-1) is 9
(1,0) is 2
(1,1) is 3
(1,2) is 14
(2,-1) is 10
(2,0) is 11
(2,1) is 12
(2,2) is 13
[root@mip-123456 163]#