LightOJ - 1337 The Crystal Maze


输出输入较大,注意用scanf,printf,否则可能超时


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
//#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <string>
using namespace std;
#define ll long long
const int maxn = 505;

char map[maxn][maxn];
bool vis[maxn][maxn];
int n, m, q, x1[maxn*maxn], y1[maxn*maxn], top, ans, an[maxn][maxn];
int dis[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

void dfs(int x, int y) {
        vis[x][y] = 1;
        if (map[x][y] == 'C') ans ++;
        x1[top] = x, y1[top ++] = y;
        for (int i = 0; i < 4; i ++) {
                int xx = x + dis[i][0];
                int yy = y + dis[i][1];
                if (xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && map[xx][yy] != '#') {
                        dfs(xx, yy);
                }
        }
        return ;
}

int main()
{
        int t, Case = 0;
        scanf("%d", &t);
        while (t --) {
                scanf("%d%d%d", &n, &m, &q);
                memset(vis, 0, sizeof(vis));
                memset(an, -1, sizeof(an));
                for (int i = 0; i < n; i ++) {
                        scanf("%s", map[i]);
                }
                int x, y;
                printf("Case %d:\n", ++ Case);
                for (int i = 0; i < q; i ++) { 
                        scanf("%d%d", &x, &y);
                        if (an[x - 1][y - 1] == -1) {
                                top = 0, ans = 0, dfs(x - 1, y - 1);
                        }
                        for (int j = 0; j < top; j ++) {
                                an[x1[j]][y1[j]] = ans;
                        }
                        printf("%d\n", an[x - 1][y - 1]);
                }
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值