POJ1979_Red and Black(JAVA语言)
思路:bfs裸题。
对这种迷宫问题的bfs,我们把坐标点用一个class来存储,并放入队列进行求解。
//一直接收不了输入,找了一个多小时的问题,居然是行和列搞反了ORZ
Red and Black
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 48833 | Accepted: 26054 |
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)
The end of the input is indicated by a line consisting of two zeros.
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
Source
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Point{
public int x;
public int y;
}
public class Main {
static int ans=0;//数量初始化为0
static int[][] dir={{0,1},{0,-1},{-1,0},{1,0}};//右左上下四个方向
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();//列数
int m=in.nextInt();//行数
Queue<Point> q=new LinkedList<Point>();//利用队列实现bfs
while(m!=0&&n!=0){//不为0时读入
char gra[][]=new char[m][n];//存迷宫
boolean vis[][]=new boolean[m][n];//标记是否访问
Point p=new Point();//表示坐标点位置
for(int i=0;i<m;i++){
String s=in.next();
for(int j=0;j<s.length();j++){
gra[i][j]=s.charAt(j);
if(gra[i][j]=='@'){ //找到@起始的位置
p.x=i;//x,y为搜索起点
p.y=j;
}
}
}
bfs(gra,vis,p,m,n,q);//调用
System.out.println(ans+1);//输出答案
ans=0;//恢复为0,为下一组做准备
q.clear();//清空队列
n=in.nextInt();//读入列
m=in.nextInt();//读入行
}
}
private static void bfs(char[][] gra, boolean vis[][],Point p, int m, int n,Queue q) {
// TODO Auto-generated method stub
vis[p.x][p.y]=true;//将起始点标记为已访问
q.add(p);//将起始点放入队列中
while(!q.isEmpty())//队列不为空时
{
Point qq=(Point) q.remove();//取出第一个元素
for(int i=0;i<4;i++)//枚举该点附近所有能到达的点
{
Point pp=new Point();
pp.x=qq.x+dir[i][0];
pp.y=qq.y+dir[i][1];
if((!(pp.x<0||pp.x>=m||pp.y<0||pp.y>=n))&&vis[pp.x][pp.y]==false&&gra[pp.x][pp.y]=='.')//如果点合法
{
vis[pp.x][pp.y]=true;//标记已访问
q.add(pp);//将该点加入队列中
ans++;//可达点数目+1
}
}
}}
}
POJ1979_Red and Black(JAVA语言)的更多相关文章
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
- Atitit onvif协议获取rtsp地址播放java语言 attilx总结
Atitit onvif协议获取rtsp地址播放java语言 attilx总结 1.1. 获取rtsp地址的算法与流程1 1.2. Onvif摄像头的发现,ws的发现机制,使用xcf类库1 2. 调用 ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- Java语言中的面向对象特性总结
Java语言中的面向对象特性 (总结得不错) [课前思考] 1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类? 2. 面向对象编程的特性有哪三个?它们各自又有哪些特性? 3. 你知 ...
- JAVA语言搭建白盒静态代码、黑盒网站插件式自动化安全审计平台
近期打算做一个插件化的白盒静态代码安全审计自动化平台和黑盒网站安全审计自动化平台.现在开源或半开源做黑盒网站安全扫描的平台,大多是基于python脚本,安全人员贡献python脚本插件增强平台功能.对 ...
- 关于Java语言和面向对象记录
本科时常用的c语言是面向过程的语言,而Java是面向对象的语言 Java语言的11个关键术语 简单性.可移植性.面向对象.分布式.高性能.解释型.健壮性.多线程.安全性.动态性.体系结构中立 面向对象 ...
- 用Java语言编写一个简易画板
讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...
- 【百度文库课程】Java语言基础与OOP入门学习笔记一
一. Java的历史与由来 原名Oak,针对嵌入式系统开发设计,语法与C/C++基本一致 二. Java语言特点 Java由四方面组成:Java编程语言.Java类文件格式.Java虚拟机和Java应 ...
- 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词
第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...
随机推荐
- 编程方式建视频——GitHub 热点速览 v.21.07
作者:HelloGitHub-小鱼干 假期过半,大家过得如何,吃好喝好了吗?GitHub 很好!本周的 GitHub Trending 又上爆款项目--github1s 装完之后,一秒 GitHub ...
- WebSocket All In One
WebSocket All In One WebSocket heartbeat WebSocket 心跳检测 ping pong refs xgqfrms 2012-2020 www.cnblogs ...
- MDN & JavaScript 文档翻译状态
MDN & JavaScript 文档翻译状态 https://developer.mozilla.org/zh-CN/docs/MDN/Doc_status/JavaScript refs ...
- Typescript & React & Vue
Typescript & React & Vue Typescript & React https://facebook.github.io/create-react-app/ ...
- vuex bug & vue computed setter
vuex bug & vue computed setter https://vuejs.org/v2/guide/computed.html#Computed-Setter [Vue war ...
- lms微服务框架介绍
lms 框架简介 Lms是一个旨在通过.net平台快速构建微服务开发的框架.具有稳定.安全.高性能.易扩展.使用方便的特点.lms内部通过dotnetty实现高性能的rpc通信,使用zookeeper ...
- C++算法代码——求数列[coci2014/2015 contest #1]
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1815 题目描述 Mirko在数学课上以一种有趣的方式操作数列,首先,他写下一个数列A ...
- node_puppeteer无界爬虫
环境:node----v14.5.0 vscode----2019 依赖库 (需要自行设置好目录结构,否则会报目录错误) const puppeteer = require("puppete ...
- 【Azure 云服务】如何从Azure Cloud Service中获取项目的部署文件
问题描述 在历史已经部署的云服务(Azure Cloud Service)中,如何获取到项目在很久以前的部署包文件呢? 解决办法 1)如果部署云服务是通过门户上传部署包到存储账号中,则可以直接从存储账 ...
- es初步搭建
1.es tar包传至linux上 并解压 tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz 2.新建用户 useradd xxxname passw ...