栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2013 Labyrinth

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2013 Labyrinth

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define maxn 1005struct Point{    int x, y;}q[maxn * maxn];int n, m;char map[maxn][maxn];int dist[maxn][maxn];int dir[4][2] = {{1, 0},{0, 1},{-1, 0},{0, -1}};void input(){    scanf("%d%d", &m, &n);    for (int i = 0; i < n; i++)        scanf("%s", map[i]);}bool in(Point b){    return b.x < n && b.y < m && b.x >= 0 && b.y >= 0;}void bfs(int sx, int sy){    int front = 0;    int rear = 0;    Point a;    a.x = sx;    a.y = sy;    q[rear++] = a;    dist[a.x][a.y] = 0;    while (front != rear)    {        a = q[front++];        if (front == maxn * maxn) front = 0;        for (int i= 0; i < 4; i++)        { Point b = a; b.x += dir[i][0]; b.y += dir[i][1]; if (in(b) && map[b.x][b.y] == '.' && dist[b.x][b.y] == -1) {     dist[b.x][b.y] = dist[a.x][a.y] + 1;     q[rear++] = b;     if (rear == maxn * maxn)         rear = 0; }        }    }}void work(){    memset(dist, -1, sizeof(dist));    for (int i = 0; i < n; i++)    {        bool did = false;        for (int j = 0; j < m; j++) if (map[i][j] == '.') {     bfs(i, j);     did = true;     break; }        if (did) break;    }    int temp = -1;    int sx = 0, sy = 0;    for (int i = 0; i < n; i++)        for (int j = 0; j < m; j++) if (dist[i][j] > temp) {     temp = dist[i][j];     sx = i;     sy = j; }    memset(dist, -1, sizeof(dist));    if (temp == -1)    {        printf("Maximum rope length is %d.n", 0);        return;    }    bfs(sx, sy);    temp = -1;    for (int i = 0; i < n; i++)        for (int j = 0; j < m; j++) if (dist[i][j] > temp)     temp = dist[i][j];    printf("Maximum rope length is %d.n", temp);}int main(){    int t;    scanf("%d", &t);    while (t--)    {        input();        work();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号