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;
}


0 0
原创粉丝点击