找规律水题。。。
Traveling Cellsperson
64-bit integer IO format: %lld Java class name: Main
You have solved every problem from Project Euler in your head. Now it is time for a problem you might have heard of,namely The Traveling Salesperson, whose decision version is NP-complete. We consider the Traveling Salesperson problem in a 2D rectangular grid where every cell can be reached from their neighboring cells (up,down, left and right) and you can visit a cell as many times as you like (though, most of the cells aren't that interesting, so you might prefer not to visit them a lot).
Input
The first line of the input consists of a single integer T, the number of test cases. Then follow two integers X and Y , marking the width and height of the grid, respectively. Then follow Y lines with X characters, where the character 'C' is a cell and the character 'S' is the starting point.
0 < T <= 50
0 < X <= 100
0 < Y <= 100
All characters in a test case are 'C', except for exactly on
Output
For each test case, output the minimum number of steps required to make a full roundtrip of the grid, starting and ending at S, and visiting each cell at least on
Since you realize that this won't lead anywhere, finish off the output with "LOL"(without quotes) on a line of its own (on
Sample Input
1
4 4
CCCC
CCCC
CSCC
CCCC
Sample Output
16
LOL
Source
IDI Open 2013 Programming Contest
#include <iostream> #include <cstring> #include <cstdio> using namespace std; char str[110]; int main() { int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%s",str); if(n>m) swap(n,m); if(n==1) { printf("%d\n",2*m-2); } else { if(n%2==0||m%2==0) { printf("%d\n",n*m); } else { printf("%d\n",m*n+1); } } } puts("LOL"); return 0; } |