编程介的小学生 2016-12-26 14:13 采纳率: 20.5%
浏览 1108
已采纳

The Traveling Salesman

Description

For thousands of years, salesmen have faced the problem of touring sites for potential customers, visiting each site exactly once, and minimizing the total cost of this tour. In the old days, these sites were towns spread out throughout the country, and the salesman had to travel the rickety, crooked roads that connected the towns. But now, in 2030, cities and have modernized and grown, so a salesman typically stays within the same city. Furthermore, modern cities are laid out in a grid-like pattern of tall skyscrapers, with sky bridges joining some pairs of adjacent skyscrapers on all floors.

The salesman has determined which floor of each skyscraper contains the most potential customers and will only visit the best floor of each skyscraper. Now the salesman must plan his course. Starting on the ground floor (floor 0) of the skyscraper in the northwest corner of the grid, the salesman must visit the specified best floor of each skyscraper before returning to the ground floor of either of the skyscrapers at the southwest, southeast, or northeast corners. In order to travel between floor 10 of one skyscraper and floor 6 of an adjacent skyscraper, the salesman first takes the sky bridge across to floor 10 of the adjacent skyscraper and then takes an elevator down 4 floors. Of course, because the salesman’s time is precious, he wants to minimize the total number of floors he must travel by elevator. For example, if his tour involves traveling from floor 3 to 8 to 5 to 10, the number of floors traveled by elevator is (8 − 3) + (8 − 5) + (10 − 5) = 13.

To make the salesman’s life (and yours) more difficult, some sky bridges do not exist between two adjacent skyscrapers, and in that case, obviously the salesman cannot fly across the gap.

Because modern cities are much larger than the small towns that salesmen dealt with generations ago, our salesman needs a plan so that he does not lose his bearings and visit a skyscraper more than once (in which case he will get beaten by the people for pestering them so much) or less than once (in which case he might have lost profit). To that end, the salesman has decided to only consider tours similar to the one in the figure. The salesman starts at the northwest corner, and either goes south or east for any distance (as long as all sky bridges exist along that path). Then, he turns towards away from the edge of the city and travels to the adjacent skyscraper, and turns again, heading back opposite the initial direction. After any amount of zig-zagging, the salesman can choose to switch over to zig-zagging in the other orientation.

Given the floors of each skyscraper in the city that the salesman wishes to visit and which sky bridges do not exist, compute the minimum number of floors that the salesman must travel on a tour that meets the above criteria. Also, print out the number of possible tours achieving that minimum.

Input

The first line contains two numbers M (1 ≤ M ≤ 1000) and N (1 ≤ N ≤ 1000), the dimensions of the city. Each of the next M lines contains N integers in the range [0, 100]. Each integer is the floor that the salesman must visit. Optionally following each integer might be a token specifying that some sky bridges do not exist. Suppose you just read the floor for location (x, y) (note that (0, 0) is the northwest corner, (N − 1, 0) is the northeast corner, (0, M − 1) is the southwest corner, and (N − 1, M − 1) is the southeast). If the token is x, no sky bridges exist between (x, y) and (x + 1, y). If the token is y, no sky bridges exist between (x, y) and (x, y + 1). All tokens and integers are separated by a single space.

Output

If there is no tour, print No solution. Otherwise, print out the number of tours and the minimum number of floors travelled.

Sample Input

2 4
0 y 10 20 30
5 8 25 28
Sample Output

1 tours, traveling a minimum of 60 total floors

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-26 16:14
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)