UVa 10496 - Collecting Beepers

來源:互聯網
上載者:User

標籤:blog   io   os   sp   for   2014   log   bs   cti   

題目:一個機器人從一個起始點出發(只能上、下、左、右運動),經過一些點後回到起點,求總路徑最小長度。

分析:圖論,搜尋。兩點間的距離為:abs(x1-x2)+ abs(y1-y2);每個點必須至少經過一次。

            如果存在一個點走過多次,那麼他一定在其他兩點間的路徑上,則這個點可以不經過這麼多次;

            因此我們只考慮每個點經過一次的情況即可(可能存在點在某兩點路徑上),求出全排列,枚舉最優解。

說明:過度自信的起源難道是自卑╮(╯▽╰)╭。

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;int  save[3628800][11],temp[11],used[11],Count;void dfs(int d, int n){if (d == n) {for (int i = 0 ; i < d ; ++ i)save[Count][i] = temp[i];Count ++;return;}for (int i = 0 ; i < n ; ++ i)if (!used[i]) {used[i] = 1;temp[d] = i;dfs(d+1, n);temp[d] = i;used[i] = 0;}}int x[11],y[11];int main(){int T,N,M,s_x,s_y,D;while (cin >> T)while (T --) {cin >> N >> M >> s_x >> s_y >> D;for (int i = 0 ; i < D ; ++ i)cin >> x[i] >> y[i];Count = 0;dfs(0, D);int Min = 444444;for (int i = 0 ; i < Count ; ++ i) {int dist = abs(s_x-x[save[i][0]]) + abs(s_y-y[save[i][0]]);for (int j = 1 ; j < D ; ++ j)dist += abs(x[save[i][j-1]]-x[save[i][j]]) + abs(y[save[i][j-1]]-y[save[i][j]]);dist += abs(s_x-x[save[i][D-1]]) + abs(s_y-y[save[i][D-1]]);Min = min(Min, dist);}cout << "The shortest path has length " << Min << endl;}     return 0;}


UVa 10496 - Collecting Beepers

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.