BNU33653:Traveling Cellsperson

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 fi rst 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 one, which is 'S'.

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 once.
Since you realize that this won't lead anywhere, fi nish off the output with "LOL"(without quotes) on a line of its own (one per run, not per test case).

Sample Input

1
4 4
CCCC
CCCC
CSCC
CCCC

Sample Output

16
LOL

 

给你一个图,要求从s开始遍历所有c回到s最少的步数

我们可以直到当n,m其中一个为1的时候,步数必为另一个变量*2-2

当两者最少有一个偶数的时候,就是m*n

当两者都为奇数,那么就要多走一步

 

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

char s[10000];
int main()
{
    int i,j,n,m,t,w;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0; i<m; i++)
            scanf("%s",s);
        int x=m*n;
        if(n == 1)
            printf("%d\n",m*2-2);
        else if(m == 1)
            printf("%d\n",n*2-2);
        else if(m%2&&n%2)
            printf("%d\n",x+1);
        else printf("%d\n",x);

    }
    printf("LOL\n");
    return 0;
}


 

你可能感兴趣的:(水,BNU)