BNU33653:Traveling Cellsperson
来源:互联网 发布:凡科域名注册 编辑:程序博客网 时间:2024/04/30 02:57
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;}
- BNU33653:Traveling Cellsperson
- UESTC 1852 Traveling Cellsperson
- UESTC 1852 Traveling Cellsperson
- IDIOPEN 2013 Traveling Cellsperson
- Traveling
- Traveling
- Traveling Light
- Traveling light
- Traveling Light - 轻装前行
- jTibet Traveling Tibet
- Kernel Packet Traveling Diagram
- Kernel Packet Traveling Diagram
- Poj3033 Traveling Salesman(搜索)
- The Traveling Salesman Problem
- 2014辽宁省赛 Traveling
- POJ2686 Traveling by Stagecoach
- Guiyang, Diverse traveling experience
- Traveling 【图的性质】
- 用多线程加速快排(方式一)
- poj1088 滑雪 dp记忆化搜索 dfs深度优先搜索
- 震撼!!
- java对象初始化顺序
- hdu 3849 By Recognizing These Guys (割边/桥)
- BNU33653:Traveling Cellsperson
- C++通过new创建对象的浅析
- poj2774,hdu1403 lcs 后缀数组,后缀自动机
- jhbuild安装笔记
- 高级开发人员面试宝典-算法
- BNU33656:C.S.I.: P15(模拟)
- Oracle11g R2 Datebase Control-orcl不能登录
- Android 漫游之路------SharedPreferences、Xml、Xml的pull解析
- android 下拉刷新