ural 1338. Automobiles
1338. Automobiles
Time limit: 3.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Everyone knows what traffic congestion is. Even supercomputer won’t be able to lay an optimal passage of the city center in the rush-hours. But it is possible to simulate the traffic flows.
For that the following experiment is carried out. We choose several check points on the streets net of the city and one goal T from the checkpoints. We send a car from each checkpoint (except T) to the point T along the shortest way. In the point T we fix how many cars came from the North, how many from the South, from the East and from the West. So we can judge about the functioning capacity of the approaches to the point T.
You are to carry out such an experiment. No, you are not given a car! You are no to go anywhere. You are only to write a program simulating the experiment.
Input
Input contains the city plan description in the following format. The first line contains two integers W and H (1 ≤ W, H ≤ 500) – the width and the height of the plan. The next H lines consists of the streets net and checkpoints description. A symbol “.” means a place where a building is. A symbol “#” means a road fragment. A symbol “o” (small Latin letter) means a checkpoint. A road fragment always occupy a cell completely. Two road fragments belong to one road if and only if they have a common side.
Then there is a series of assignments of the experiment. First of all there is a number of assignments M (0 ≤ M ≤ 20). Each of the next M lines contains the number of the goal point T for the corresponding experiment. Assume that the checkpoints are numbered bottom-up and from left to right.
If some car is to choose a way from several shortest ones the next scheme of priorities acts: South, North, West, East.
Output
You are to output the results of each experiment in the following format:
Experiment #N: North: Rn, South: Rs, East: Re, West: Rw
where Rn, Rs, Re and Rw - an amount of cars that came in the experiment number N to the goal point from the North, South, East and West respectively.
Sample
| input | output |
|---|---|
10 5 ..####.... ..o..o.... ..####.#o. ......##.. .o#####... 1 4 |
Experiment #1: North: 0, South: 1, East: 0, West: 0 |
Problem Author: Alexander Klepinin
Problem Source: USU Championship 2004
Problem Source: USU Championship 2004
Tags: graph theory
Difficulty: 1752
题意:给出一个图o为检查点,可走,.不可走,#可走
首先要求最短,其次有一个对于方向的优先顺序South, North, West, East.
问对于某个检查点,其他检查点按照上述规则走,是从哪个方向走到这个点。
分析:
这题居然是居然难度这么高。。。
不就是bfs吗。。
注意优先顺序
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 inline void SetIO(string Name) 38 { 39 string Input = Name+".in", 40 Output = Name+".out"; 41 freopen(Input.c_str(), "r", stdin), 42 freopen(Output.c_str(), "w", stdout); 43 } 44 45 46 inline int Getint() 47 { 48 int Ret = 0; 49 char Ch = ' '; 50 bool Flag = 0; 51 while(!(Ch >= '0' && Ch <= '9')) 52 { 53 if(Ch == '-') Flag ^= 1; 54 Ch = getchar(); 55 } 56 while(Ch >= '0' && Ch <= '9') 57 { 58 Ret = Ret * 10 + Ch - '0'; 59 Ch = getchar(); 60 } 61 return Flag ? -Ret : Ret; 62 } 63 64 const int N = 510; 65 const int Dx[] = {1, -1, 0, 0}, Dy[] = {0, 0, -1, 1}; 66 const int Where[] = {1, 0, 3, 2}; 67 int m, n; 68 char Map[N][N]; 69 typedef pair<int, int> II; 70 II Point[N * N], Que[N * N * 4]; 71 int Tot; 72 int Dp[N][N], Mark[N][N], Come[N][N]; 73 74 inline void Input() 75 { 76 scanf("%d%d", &m, &n); 77 For(i, 1, n) 78 { 79 getchar(); 80 scanf("%s", Map[i] + 1); 81 } 82 } 83 84 inline bool Check(int x, int y) 85 { 86 if(x < 1 || x > n || y < 1 || y > m) return 0; 87 if(Map[x][y] == '.') return 0; 88 return 1; 89 } 90 91 inline void Bfs(int Stx, int Sty) 92 { 93 For(i, 1, n) 94 For(j, 1, m) 95 Dp[i][j] = INF, Mark[i][j] = Come[i][j] = -1; 96 int Tx, Ty, Head = 1, Tail = 0; 97 Rep(i, 4) 98 { 99 Tx = Stx + Dx[i]; 100 Ty = Sty + Dy[i]; 101 if(Check(Tx, Ty)) 102 { 103 Dp[Tx][Ty] = 1; 104 Mark[Tx][Ty] = i; 105 Come[Tx][Ty] = Where[i]; 106 Que[++Tail] = mk(Tx, Ty); 107 } 108 } 109 110 while(Head <= Tail) 111 { 112 II Now = Que[Head++]; 113 int x = Now.ft, y = Now.sd; 114 Rep(i, 4) 115 { 116 Tx = x + Dx[i]; 117 Ty = y + Dy[i]; 118 if(Check(Tx, Ty)) 119 { 120 if(Dp[Tx][Ty] > Dp[x][y] + 1 || 121 (Dp[Tx][Ty] == Dp[x][y] + 1 && Come[Tx][Ty] > Where[i])) 122 { 123 Dp[Tx][Ty] = Dp[x][y] + 1; 124 Mark[Tx][Ty] = Mark[x][y]; 125 Come[Tx][Ty] = Where[i]; 126 Que[++Tail] = mk(Tx, Ty); 127 } 128 } 129 } 130 } 131 } 132 133 inline void Solve() 134 { 135 Ford(i, n, 1) 136 For(j, 1, m) 137 if(Map[i][j] == 'o') 138 Point[++Tot] = mk(i, j); 139 140 int Opt, Ans[4]; 141 scanf("%d", &Opt); 142 For(Test, 1, Opt) 143 { 144 int x; 145 scanf("%d", &x); 146 Bfs(Point[x].ft, Point[x].sd); 147 148 Rep(i, 4) Ans[i] = 0; 149 For(i, 1, Tot) 150 if(x != i && Mark[Point[i].ft][Point[i].sd] != -1) 151 Ans[Mark[Point[i].ft][Point[i].sd]]++; 152 printf("Experiment #%d: North: %d, South: %d, East: %d, West: %d\n", 153 Test, Ans[1], Ans[0], Ans[3], Ans[2]); 154 } 155 } 156 157 int main() 158 { 159 #ifndef ONLINE_JUDGE 160 SetIO("A"); 161 #endif 162 Input(); 163 Solve(); 164 return 0; 165 }

浙公网安备 33010602011771号