loj 1337
来源:互联网 发布:java图书管理系统教程 编辑:程序博客网 时间:2024/05/22 07:43
题目链接:http://lightoj.com/volume_showproblem.php?problem=1337
思路:对于搜过的区域进行标记,如果要求的点落在已经搜过的区域,那么直接取出来即可,否则,就dfs一下。
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 9 const int MAXN = (500 + 50);10 int n, m, Q, _count, cnt;11 char map[MAXN][MAXN];12 int mark[MAXN][MAXN];13 vector<int >ans;14 int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1} };15 16 void dfs(int x, int y)17 {18 mark[x][y] = _count;19 if (map[x][y] == 'C') cnt++;20 for (int i = 0; i < 4; i++) {21 int xx = x + dir[i][0];22 int yy = y + dir[i][1];23 if (xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy] != '#') {24 if (mark[xx][yy] == -1)dfs(xx, yy);25 }26 }27 }28 29 30 int main()31 {32 int _case, t = 1;33 scanf("%d", &_case);34 while (_case--) {35 scanf("%d %d %d", &n, &m, &Q);36 for (int i = 0; i < n; i++) {37 scanf("%s", map[i]);38 }39 memset(mark, -1, sizeof(mark));40 ans.clear();41 _count = -1;42 printf("Case %d:\n", t++);43 while (Q--) {44 int x, y;45 scanf("%d %d", &x, &y);46 x--, y--;47 if (map[x][y] == '#') {48 puts("0");49 }50 else if (mark[x][y] == -1) {51 _count++;52 cnt = 0;53 dfs(x, y);54 ans.push_back(cnt);55 printf("%d\n", cnt);56 }57 else {58 printf("%d\n", ans[mark[x][y]]);59 }60 }61 }62 return 0;63 }
0 0
- loj 1337
- LOJ 1060
- LOJ 1119
- loj 1271
- LOJ 1070
- Loj #114
- loj 1224 - DNA Prefix
- LOJ 1307 Counting Triangles
- loj 1063(求割点个数)
- loj 1009(dfs)
- loj 1046(bfs)
- loj 1055(bfs)
- loj 1185(bfs)
- loj 1168(Tarjan应用)
- loj 1004(dp)
- loj 1017(dp)
- loj 1032(数位dp)
- loj 1036(dp)
- [LeetCode]Next Permutation
- loj1011 状态压缩
- loj 1030概率dp
- loj 1032 数位dp
- loj 1257 (求树上每一个点到树上另一个点的最长距离)
- loj 1337
- loj 1357(树形dp)
- loj 1210 (求最少的加边数使得图变成强连通)
- loj 1406(状态压缩)
- loj 1429(可相交的最小路径覆盖)
- poj上的dp专题
- poj 3895(求无向图的最大简单环)
- spfa求最长路
- FANN的图形用户界面形式