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

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
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 = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
const int Dx[] = {, -, , }, Dy[] = {, , -, };
const int Where[] = {, , , };
int m, n;
char Map[N][N];
typedef pair<int, int> II;
II Point[N * N], Que[N * N * ];
int Tot;
int Dp[N][N], Mark[N][N], Come[N][N]; inline void Input()
{
scanf("%d%d", &m, &n);
For(i, , n)
{
getchar();
scanf("%s", Map[i] + );
}
} inline bool Check(int x, int y)
{
if(x < || x > n || y < || y > m) return ;
if(Map[x][y] == '.') return ;
return ;
} inline void Bfs(int Stx, int Sty)
{
For(i, , n)
For(j, , m)
Dp[i][j] = INF, Mark[i][j] = Come[i][j] = -;
int Tx, Ty, Head = , Tail = ;
Rep(i, )
{
Tx = Stx + Dx[i];
Ty = Sty + Dy[i];
if(Check(Tx, Ty))
{
Dp[Tx][Ty] = ;
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, )
{
Tx = x + Dx[i];
Ty = y + Dy[i];
if(Check(Tx, Ty))
{
if(Dp[Tx][Ty] > Dp[x][y] + ||
(Dp[Tx][Ty] == Dp[x][y] + && Come[Tx][Ty] > Where[i]))
{
Dp[Tx][Ty] = Dp[x][y] + ;
Mark[Tx][Ty] = Mark[x][y];
Come[Tx][Ty] = Where[i];
Que[++Tail] = mk(Tx, Ty);
}
}
}
}
} inline void Solve()
{
Ford(i, n, )
For(j, , m)
if(Map[i][j] == 'o')
Point[++Tot] = mk(i, j); int Opt, Ans[];
scanf("%d", &Opt);
For(Test, , Opt)
{
int x;
scanf("%d", &x);
Bfs(Point[x].ft, Point[x].sd); Rep(i, ) Ans[i] = ;
For(i, , Tot)
if(x != i && Mark[Point[i].ft][Point[i].sd] != -)
Ans[Mark[Point[i].ft][Point[i].sd]]++;
printf("Experiment #%d: North: %d, South: %d, East: %d, West: %d\n",
Test, Ans[], Ans[], Ans[], Ans[]);
}
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("A");
#endif
Input();
Solve();
return ;
}

ural 1338. Automobiles的更多相关文章

  1. [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)

    再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...

  2. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  3. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  4. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  5. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  6. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  7. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  8. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  9. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

随机推荐

  1. Eclipse的各种快捷键

     参考他人总结好的 Eclipse快捷键大全(转载) Ctrl+1 快速修复(最经典的快捷键,就不用多说了)     ---->例如:add unimplement methodCtrl+D:  ...

  2. Python 小游戏 Bunny

    最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...

  3. Shell编程基础教程6--shell函数

    6.shell函数    6.1.定义函数        简介:            shell允许将一组命令集或语句形成一个可用块,这些块成为shell函数        定义函数的格式      ...

  4. SQL语法中的子查询Subqueries

    记一下样子. 明白它的应用场景. SELECT account_id, product_cd, cust_id, avail_balance FROM account WHERE open_emp_i ...

  5. android 入门-android属性介绍

      android:visibility="gone" 不保留view控件所占有的空间 隐藏 android:visibility="invisible" 保留 ...

  6. [SQL]查询及删除重复记录的SQL语句

    一:查询及删除重复记录的SQL语句1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select ...

  7. JavaScript前端框架的思考

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:现在JavaScript前端框架层出不穷,尤其Angular进入到2.x时候之后,我们 ...

  8. C可变参数函数 实现

    转自:http://blog.csdn.net/weiwangchao_/article/details/4857567 C函数要在程序中用到以下这些宏: void va_start( va_list ...

  9. linux脚本编程技术

    linux脚本编程技术 一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格 ...

  10. Sizeof与Strlen的区别与联系(转)

    Sizeof与Strlen的区别与联系 一.sizeof     sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...