标签:style post system.in point sem arch whether target static
题意:用广度优先搜索
//c++写输入时有问题
1)这个是深搜
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*
map数组是用来装字符的
n,m提高作用域,使訪问的权限变高
dir方便广度优先搜索,由于要搜索8个方向。这样做最方便
*/
char map[101][101];
int n,m;
int dir[8][2]={{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};
int main(){
void breadthFirstSearch(int x,int y);
bool isWithinMap(int x,int y);
while(scanf("%d%d",&m,&n)!=EOF,n+m){
/*
m是行指标
n是列指标
*/
getchar();
for(int i=0;i<m;i++){
scanf("%s",map[i]);
}
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(map[i][j]=='@'){
/*
擦掉当前点
*/
breadthFirstSearch(i,j);
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}
/*
px-pointx
py-pointy
breadthFirstSearch广搜优先搜索
*/
void breadthFirstSearch(int x,int y){
bool isWithinMap(int x,int y);
int px,py;
for(int i=0;i<8;i++){
px = x+dir[i][0];
py = y+dir[i][1];
if(isWithinMap(px,py)&&map[px][py]=='@'){
/*
1)擦掉原来的@
2)广搜该点
*/
map[px][py] = '*';
breadthFirstSearch(px,py);
}
}
}
bool isWithinMap(int x,int y){
if(x<0||x>=m||y<0||y>=n){
return false;
}
return true;
}
2)java广搜
import java.util.Scanner;
/*
* 使用广搜实现
*/
public class p1241 {
/*
* 相当于全局变量,降低内存的使用
*/
static Plot[][] plots = new Plot[101][101];
static Queue queue = new Queue();
static int dir[][] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 },
{ 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } };
static int m = 0;
static int n = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 0;
while (sc.hasNext()) {
/*
* m控制行
* n控制列
*/
m = sc.nextInt();
n = sc.nextInt();
if (m + n == 0) {
return;
}
for (int i = 0; i < m; i++) {
String str = sc.next();
for (int j = 0; j < n; j++) {
plots[i][j] = new Plot(i, j);
plots[i][j].ch = str.charAt(j);
}
}
count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (plots[i][j].ch == '@') {
/*
* 通过广搜把相连的油田改变成"*"
* 这样就能够降低訪问次数了
*/
plots[i][j].ch = '*';
queue.add(plots[i][j]);
breadthFirthSearch();
count++;
}
}
}
System.out.println(count);
}
}
/*
*
*/
private static void breadthFirthSearch() {
Plot plot = null;
int px = 0;
int py = 0;
while (!queue.isEmpty()) {
plot = queue.pop();
for (int i = 0; i < 8; i++) {
px = plot.x + dir[i][1];
py = plot.y + dir[i][0];
if (isWithinMap(px, py) && plots[px][py].ch == '@') {
plots[px][py].ch = '*';
queue.add(plots[px][py]);
}
}
}
}
/*
* x表示行值
* y表示列值
*/
private static boolean isWithinMap(int x, int y) {
if (x < 0 || y < 0 || x >= m || y >= n) {
return false;
}
return true;
}
}
/*
* plot油井
*/
class Plot {
/*
* 这道题思想简单化,没有写visited,parent
* 你懂得。不用的变量不写才是高效的。
*/
char ch;
int x;
int y;
public Plot(int x, int y) {
this.x = x;
this.y = y;
}
}
/*
* 建立自己的队列
*/
class Queue {
final int FRONT = 0;
Plot[] plots;
int end;
/*
* 最多包括100块油井
*/
public Queue() {
plots = new Plot[101];
end = 0;
}
/*
* 入栈函数
*/
public void add(Plot plot) {
plots[end] = plot;
end++;
}
/*
* 出栈函数
*/
public Plot pop() {
if (end <= 0) {
return null;
}
Plot plot = plots[FRONT];
for (int i = 0; i < end; i++) {
plots[i] = plots[i + 1];
}
end--;
return plot;
}
/*
* 推断栈是否为空
*/
public boolean isEmpty() {
if (end >= 1) {
return false;
}
return true;
}
}
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
标签:style post system.in point sem arch whether target static
原文地址:http://www.cnblogs.com/mfmdaoyou/p/7202868.html