题意:在一个n*m的网格中放尽可能多的皇后,使他们不会互相攻击。
思路:根据样例,推出规律。http://hi.baidu.com/knowledgetime/item/87d13b1f12290a53f1090ea7
启发:这种题,应该根据样例,摸索出基本的规律,然后分析细节。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #include<cmath> #define LL long long #define ULL unsigned long long using
namespace
std;
int
main()
{ // freopen("in.txt","r",stdin); int
m,n;
while ( scanf ( "%d%d" ,&m,&n)&&(m+n))
{
if (m==1)
printf ( "%d knights may be placed on a %d row %d column board.\n" ,n,m,n);
else
if (n==1)
printf ( "%d knights may be placed on a %d row %d column board.\n" ,m,m,n);
else
if (m==2)
printf ( "%d knights may be placed on a %d row %d column board.\n" ,n/4*4+(n%4>=2?4:n%4*2),m,n);
else
if (n==2)
printf ( "%d knights may be placed on a %d row %d column board.\n" ,m/4*4+(m%4>=2?4:m%4*2),m,n);
else
printf ( "%d knights may be placed on a %d row %d column board.\n" ,(m*n+1)/2,m,n);
}
return
0;
} |