Problem 81 » Red and Black

本文介绍了一种基于深度优先搜索的迷宫寻路算法。该算法通过递归地访问迷宫中的每个黑色方块(可达区域),并在访问过的方块上标记避免重复访问,最终统计出从起始位置可达的所有方块数量。输入包括迷宫的宽度和高度及迷宫布局,输出为可达方块总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

INPUT

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)

OUTPUT

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

SAMPLE INPUT

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

SAMPLE OUTPUT

45
59
6
13

 

#include<iostream>
using namespace std;
int num,w,h;
char a[21][21];
bool f(int x,int y)
{
   if(x<0||x>=h||y<0||y>=w)
   return 0;
   else
   return 1;
}
void dps(int x,int y)
{
   if(f(x,y)&&a[x][y]=='.')
   {
     a[x][y]='#';
    num++;
    dps(x-1,y);
    dps(x+1,y);
    dps(x,y-1);
    dps(x,y+1);
   }
  
}
int main()
{
    int i,j;
 while(cin>>w>>h&&w!=0&&h!=0)
 {
  for(i=0;i<h;i++)
   for(j=0;j<w;j++)
   cin>>a[i][j];
  num=0;
  for(i=0;i<h;i++)
   for(j=0;j<w;j++)
    if(a[i][j]=='@')
    {
      a[i][j]='.';
     dps(i,j);
    }
  cout<<num<<endl;
 }

}

org.apache.jasper.JasperException: 无法为JSP编译类: 在生成的java文件中的第:[15]行发生错误:[C:\Users\19653\AppData\Local\JetBrains\IntelliJIdea2024.3\tomcat\Unnamed_meeting\work\Catalina\localhost\ROOT\org\apache\jsp\html\article_jsp.java] The type jdk.jfr.internal.tool.Command.title is not visible Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:72) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:192) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:542) org.apache.jasper.compiler.Compiler.compile(Compiler.java:371) org.apache.jasper.compiler.Compiler.compile(Compiler.java:343) org.apache.jasper.compiler.Compiler.compile(Compiler.java:329) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:376) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:324) javax.servlet.http.HttpServlet.service(HttpServlet.java:623) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51此为错误,以下为jsp代码<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <%@ page import="static jdk.jfr.internal.tool.Command.title" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>文章</title> </head> <body> <jsp:include page="/incloud/header.jsp" /> <div class="d-flex align-items-stretch"> <!-- Sidebar Navigation--> <jsp:include page="/incloud/sidebar.jsp" /> <% boolean browseCount; %> <!-- Sidebar Navigation end--> <div class="page-content"> <div class="page-header"> <div class="container-fluid"> <h2 class="h5 no-margin-bottom">发布文章</h2> </div> </div> <li class="no-padding-bottom"> <div class="list-group myList"> <!--搜索文章的条件--> <div class="myTitle"> <form class="form-inline" action="article.jsp" method="get"> <div class="form-group"> <label for="inlineFormInput" class="sr-only">Name</label> <input id="inlineFormInput" name="title" type="text" placeholder="按标题名字查找" class="mr-sm-3 form-control"> </div> <div class="form-group"> <input type="submit" value="查询" class="btn btn-primary"> </div> </form> <input id = "addArt" type="submit" value="发布文章" class="btn btn-primary"> </div> <ul> <li class="list-group-item" action="/article/findAll" method="get"> <table> <thead> <tr> <span>收藏数:100</span> <span>&nbsp;</span> <span>&nbsp;</span> <span>&nbsp;</span> <span>浏览数:<th> </th></span> <p class="h6">作者:<th> </th></p> <p class="h6">时间:<th></th></p> <p style="white-space:nowrap;overflow:hidden;text-overflow: ellipsis"><th></th></p> </tr> </thead> <tbody> <tr> <th scope="row">${article.user_id}</th> <td>${article.browse_count}</td> <td>${article.publish_realname}</td> <td>${publish_date}</td> </tr> </tbody> </table> </li> </ul> <nav class="text-center" aria-label="Page navigation"> <ul class="pagination"> <li> <a href="javascript:" onclick="pre()" aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> <c:forEach begin="1" end="${pageData.totalPage}" varStatus="i"> <li><a href="/article/findAll?currPage=${i.count}&Title=${Title}">${i.count}</a></li> </c:forEach> <li> <a href="javascript:" onclick="next()" aria-label="Next"> <span aria-hidden="true">&raquo;</span> </a> </li> </ul> </nav> </div> </li> </section> <jsp:include page="/incloud/footer.jsp" /> </div> </div> </body> </html> <script type="text/javascript"> function pre() { if (${pageData.currPage-1<=0}) { layer.msg('已经到顶啦!'); return; } window.location.href = "/user/findAll?currPage=${pageData.currPage - 1}&realName=${realName}" } //添加文章跳转链接 $("#addArt").click(function () { window.location.href = '/html/article_add.jsp' }); </script>
最新发布
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值