poj1654

给出八方向行走方案求路线圈出的面积。

计算面积时总是算二倍的面积,这样就可以处理0.5的问题。

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
#include
< cmath >
using namespace std;

int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf(
" %d " , & t);
getchar();
while (t -- )
{
long long ans = 0 ;
int x = 0 ;
int y = 0 ;
char ch;
while (ch = getchar(), ch != ' 5 ' )
{
switch (ch)
{
case ' 1 ' :
ans
-= y * 2 - 1 ;
x
-- ;
y
-- ;
break ;
case ' 2 ' :
y
-- ;
break ;
case ' 3 ' :
ans
+= y * 2 - 1 ;
x
++ ;
y
-- ;
break ;
case ' 4 ' :
ans
-= y * 2 ;
x
-- ;
break ;
case ' 6 ' :
ans
+= y * 2 ;
x
++ ;
break ;
case ' 7 ' :
ans
-= y * 2 + 1 ;
x
-- ;
y
++ ;
break ;
case ' 8 ' :
y
++ ;
break ;
case ' 9 ' :
ans
+= y * 2 + 1 ;
x
++ ;
y
++ ;
break ;
}
}
getchar();
if (ans < 0 )
ans
= - ans;
if (ans & 1 )
cout
<< ans / 2 << " .5 " << endl;
else
cout
<< ans / 2 << endl;
}
return 0 ;
}

你可能感兴趣的:(poj)