CodeForces - 598D Igor In the Museum
来源:互联网 发布:无人机数据算法 编辑:程序博客网 时间:2024/04/29 10:15
Igor is in the museum and he wants to see as many pictures as possible.
Museum can be represented as a rectangular field ofn × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.
At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.
For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.
First line of the input contains three integersn,m andk (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.
Each of the next n lines containsm symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.
Each of the last k lines contains two integersx andy (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3*******..*.********....*******2 22 54 3
6410
4 4 1*****..**.******3 2
8
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char ch[1050][1050];int vist[1050][1050], ans[100000+20], temp[1050][1050];int dx[4] = {1, 0, -1, 0};int dy[4] = {0, 1, 0, -1};int num, n, m, id;void dfs(int x, int y) { vist[x][y] = 0; for(int i = 0; i < 4; i++) { int nx = x+dx[i]; int ny = y+dy[i]; if(0<nx && nx<=n && 0<ny && ny<=m && vist[nx][ny] != 0) { if(ch[nx][ny] == '*') num++; else { temp[nx][ny] = id; //属于同一个块 dfs(nx, ny); } } }}int main(){ int t, sx, sy; scanf("%d%d%d", &n, &m, &t); getchar(); for(int i = 1; i <= n; i++) { for(int j = 1; j <=m ;j++) { scanf("%c", &ch[i][j]); } getchar(); } memset(vist, 1, sizeof(vist)); id = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(ch[i][j] == '.' && vist[i][j]) { num = 0; id++; //连通块编号 temp[i][j] = id; //此坐标对应编号 dfs(i, j); ans[temp[i][j]] = num; //id号连通块的答案 } } } while(t--) { scanf("%d%d", &sx, &sy); printf("%d\n", ans[temp[sx][sy]]); } return 0;}
- Codeforces 598D:Igor In the Museum
- codeforces 598D - Igor In the Museum
- CodeForces - 598D Igor In the Museum
- Codeforces 598D Igor In the Museum 【DFS】
- CodeForces 598D Igor In the Museum 暴力
- Codeforces 598D Igor In the Museum(概率DP)
- Codeforces 598D Igor In the Museum (dfs)
- Codeforces 598D Igor In the Museum (DFS)
- CodeForces 598D-Igor In the Museum【DFS】
- Codeforces 598D Igor In the Museum(bfs)
- CodeForces 598D Igor In the Museum【dfs】
- Codeforces 598D Igor In the Museum 离线处理+bfs
- CodeForces 598D Igor In the Museum【dfs】
- CodeForces 598 D. Igor In the Museum(dfs)
- Codeforces 598D Igor In the Museum【预处理Bfs】
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1 D.Igor In the Museum(DFS)
- hosts文件被删除可以怎么办!
- uva 1395 Slim Span (最小生成树||(最大边减最小边尽量小))
- 06-Windows Server 2012 R2 会话远程桌面-标准部署-RD网关(RemoteApp)
- 【jzoj3848】【大水题】【数位动态规划】
- Linux增加分区大小
- CodeForces - 598D Igor In the Museum
- 机器学习:代价函数cost function
- 技术人
- Swift3.0-类(class)
- Linux文件搜索find命令
- centos克隆后的网络配置
- 62 ----这个文件夹保存的格式为utf8的,否则会出不来;----文件保存格式不对也出不来:
- jquery 获取 id ,但是id 里面不能有. 这个符号
- JMX