您的位置:首页 > 其它

ural 1338. Automobiles

2015-11-26 21:55 267 查看

1338. Automobiles

Time limit: 3.0 second
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

inputoutput
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

Tags: graph theory (hide tags for unsolved problems)
Difficulty: 1752

题意:给出一个图o为检查点,可走,.不可走,#可走
首先要求最短,其次有一个对于方向的优先顺序South, North, West, East.
问对于某个检查点,其他检查点按照上述规则走,是从哪个方向走到这个点。
分析:
这题居然是居然难度这么高。。。
不就是bfs吗。。
注意优先顺序

/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
}

inline int Getint()
{
int Ret = 0;
char Ch = ' ';
bool Flag = 0;
while(!(Ch >= '0' && Ch <= '9'))
{
if(Ch == '-') Flag ^= 1;
Ch = getchar();
}
while(Ch >= '0' && Ch <= '9')
{
Ret = Ret * 10 + Ch - '0';
Ch = getchar();
}
return Flag ? -Ret : Ret;
}

const int N = 510;
const int Dx[] = {1, -1, 0, 0}, Dy[] = {0, 0, -1, 1};
const int Where[] = {1, 0, 3, 2};
int m, n;
char Map

;
typedef pair<int, int> II;
II Point[N * N], Que[N * N * 4];
int Tot;
int Dp

, Mark

, Come

;

inline void Input()
{
scanf("%d%d", &m, &n);
For(i, 1, n)
{
getchar();
scanf("%s", Map[i] + 1);
}
}

inline bool Check(int x, int y)
{
if(x < 1 || x > n || y < 1 || y > m) return 0;
if(Map[x][y] == '.') return 0;
return 1;
}

inline void Bfs(int Stx, int Sty)
{
For(i, 1, n)
For(j, 1, m)
Dp[i][j] = INF, Mark[i][j] = Come[i][j] = -1;
int Tx, Ty, Head = 1, Tail = 0;
Rep(i, 4)
{
Tx = Stx + Dx[i];
Ty = Sty + Dy[i];
if(Check(Tx, Ty))
{
Dp[Tx][Ty] = 1;
Mark[Tx][Ty] = i;
Come[Tx][Ty] = Where[i];
Que[++Tail] = mk(Tx, Ty);
}
}

while(Head <= Tail)
{
II Now = Que[Head++];
int x = Now.ft, y = Now.sd;
Rep(i, 4)
{
Tx = x + Dx[i];
Ty = y + Dy[i];
if(Check(Tx, Ty))
{
if(Dp[Tx][Ty] > Dp[x][y] + 1 ||
(Dp[Tx][Ty] == Dp[x][y] + 1 && Come[Tx][Ty] > Where[i]))
{
Dp[Tx][Ty] = Dp[x][y] + 1;
Mark[Tx][Ty] = Mark[x][y];
Come[Tx][Ty] = Where[i];
Que[++Tail] = mk(Tx, Ty);
}
}
}
}
}

inline void Solve()
{
Ford(i, n, 1)
For(j, 1, m)
if(Map[i][j] == 'o')
Point[++Tot] = mk(i, j);

int Opt, Ans[4];
scanf("%d", &Opt);
For(Test, 1, Opt)
{
int x;
scanf("%d", &x);
Bfs(Point[x].ft, Point[x].sd);

Rep(i, 4) Ans[i] = 0;
For(i, 1, Tot)
if(x != i && Mark[Point[i].ft][Point[i].sd] != -1)
Ans[Mark[Point[i].ft][Point[i].sd]]++;
printf("Experiment #%d: North: %d, South: %d, East: %d, West: %d\n",
Test, Ans[1], Ans[0], Ans[3], Ans[2]);
}
}

int main()
{
#ifndef ONLINE_JUDGE
SetIO("A");
#endif
Input();
Solve();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: