2015华为实习笨笨熊搬家交通篇JAVA/C++
来源:互联网 发布:指纹识别算法排行 编辑:程序博客网 时间:2024/05/21 18:30
请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢?
地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。
矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中:
B: 代表苯苯熊现在的房子;
H: 代表笨笨熊新的豪宅;
-: 代表可以通行的道路;
#: 代表无法通过的障碍(高山、大河等);
此外,森林里也有交通规则地:在任务位置,只能向“上、下、左、右”四个方向中的其中一个方向行走。
运行时间限制:无限制
内存限制:无限制输入:
4 // R的数值4 // C的数值,下面是地图。
--##---
B-----H
#---#--
-------输出:
Y //代表道路可达
或
N //代表道路不通
样例输入:
15-B-H#
样例输出:
Y
答案提示:
import java.util.*;class Step { int x, y, d; public Step(int x, int y, int d) { this.x = x; this.y = y; this.d = d; } public String toString() { return "[" + x + ", " + y + "]"; }}public class Main { public boolean canReachHome(char[][] board) { int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; Stack<Step> s = new Stack<>(); return path(board, move, s); } private boolean path(char[][] board, int[][] move, Stack<Step> s) { int m = board.length + 2; int n = board[0].length + 2; char[][] maze = new char[m][n]; for (int i = 0; i < n; i++) { maze[0][i] = '*'; maze[m - 1][i] = '*'; } for (int i = 0; i < m; i++) { maze[i][0] = '*'; maze[i][n - 1] = '*'; } for (int i = 1; i < m - 1; i++) { for (int j = 1; j < n - 1; j++) { maze[i][j] = board[i - 1][j - 1]; } } int tempx = 0; int tempy = 0; int finalx = 0; int finaly = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (maze[i][j] == 'B') { tempx = i; tempy = j; } if (maze[i][j] == 'H') { finalx = i; finaly = j; maze[i][j] = '-'; } } } Step temp = new Step(tempx, tempy, -1); s.push(temp); while (!s.isEmpty()) { s.pop(); if (!s.isEmpty()) { temp = s.peek(); } int x = temp.x; int y = temp.y; int d = temp.d + 1; while (d < 4) { int i = x + move[d][0]; int j = y + move[d][1]; if (maze[i][j] == '-') { temp = new Step(i, j, -1); s.push(temp); x = i; y = j; maze[x][y] = '#'; if (x == finalx && y == finaly) { return true; } else { d = 0; } } else { d++; } } } return false; } public static void main(String args[]) { Scanner cin = new Scanner(System.in); int row, col; Main m = new Main(); while (cin.hasNext()) { row = Integer.valueOf(cin.nextLine()); col = Integer.valueOf(cin.nextLine()); char[][] board = new char[row][col]; for (int i = 0; i < row; i++) { String str = cin.nextLine(); for (int j = 0; j < col; j++) { board[i][j] = str.charAt(j); } } boolean ans = m.canReachHome(board); if (ans) { System.out.println("Y"); } else { System.out.println("N"); } } }}
代码不完备,转载清水河畔
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <utility>
#include <sstream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int rr[4]={-1,0,0,1};
const int cc[4]={0,-1,1,0};
int R;
int C;
bool go(vector<vector<char> > &mp,int i,int j)
{
bool ans=false;
stack<pair<int,int> > s;
s.push(make_pair(i,j));
while(!s.empty())
{
int ii=s.top().first;
int jj=s.top().second;
s.pop();
mp[ii][jj]='*';
for(int i=0;i<4;i++)
{
int tr=ii+rr[i];
int tc=jj+cc[i];
if(tr>=0 && tc>=0 &&tr<R && tc<C && mp[tr][tc]=='H')
{
ans=true;
return ans;
}
if(tr>=0 && tc>=0 &&tr<R && tc<C && mp[tr][tc]=='-')
{
mp[tr][tc]='*';
s.push(make_pair(tr,tc));
}
}
}
return ans;
}
int main()
{
while(cin>>R>>C)
{
vector<vector<char> > mp(R,vector<char>(C));
int sr,sc,dr,dc;
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='B'){sr=i;sc=j;}
if(mp[i][j]=='H'){dr=i;dc=j;}
}
}
bool ans=go(mp,sr,sc);
if(ans)cout<<'Y'<<endl;
else cout<<'N'<<endl;
}
return 0;
}
- 2015华为实习笨笨熊搬家交通篇JAVA/C++
- 华为2015年编码大赛—笨笨熊搬家交通篇JAVA实现
- 笨笨熊搬家交通篇
- 笨笨熊搬家交通篇
- 笨笨熊搬家交通篇
- 笨笨熊搬家之交通篇
- 华为编程大赛之笨笨熊搬家交通篇答案(供参考。)
- 华为编程大赛--笨笨熊搬家
- 设计模式之State——交通篇(原创)
- 设计模式之State——交通篇
- 华为编程——笨笨熊搬家
- 华为机试之笨笨熊搬家
- 2017华为实习机试题(java)
- 华为2015年实习招聘机试
- 2015/4/15 华为实习面试
- 华为2015实习上机:字符串排序
- 2015华为暑期实习面试全过程
- 华为搬家之我见
- Linux下平均分割大文件
- ListView嵌套ListView优化
- OpenLayers开发记录(一)
- Java String类型基础
- 黑马程序员—java多线程
- 2015华为实习笨笨熊搬家交通篇JAVA/C++
- LINUX 下,编程自学七:gethostbyname
- 《禅与摩托车维修艺术》摘录(二)
- 黑马程序员_Java语法基础(二)
- 无线组网架构
- 一道淘汰85%面试者的开发者面试题
- Oracle 11gR2执行DBCA报错误
- OFDM技术原理
- 摘自河畔的零碎资料——可以日后参考