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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值