2014 WAP校园招聘笔试题

简介: 2014 WAP校园招聘笔试题 Problem's Link:   http://www.doc88.com/p-6751117015483.html   WAP公司笔试题 We are planning an orienteering game.

 2014 WAP校园招聘笔试题

Problem's Link:   http://www.doc88.com/p-6751117015483.html


 

WAP公司笔试题

We are planning an orienteering game.
The aim of this game is to arrive at the goal (G) from the start (S) with the shortest distance.
However, the players have to pass all the checkpoints (@) on the map.
An orienteering map is to be given in the following format.
########
#@....G#
##.##@##
#..@..S#
#@.....#
########
In this problem, an orienteering map is to be given.
Calculate the minimum distance from the start to the goal with passing all the checkpoints.
Specification
* A map consists of 5 characters as following.
You can assume that the map does not contain any invalid characters and
the map has exactly one start symbol 'S' and exactly one goal symbol 'G'.
* 'S' means the orienteering start.
* 'G' means the orienteering goal.
* '@' means an orienteering checkpoint.
* '.' means an opened-block that players can pass.
* '#' means a closed-block that players cannot pass.
* It is allowed to move only by one step vertically or horizontally (up, down, left, or right) to the
next block.
Other types of movements, such as moving diagonally (left up, right up, left down and right down)
and skipping one or more blocks, are NOT permitted.
* You MUST NOT get out of the map.
* Distance is to be defined as the number of movements to the different blocks.
* You CAN pass opened-blocks, checkpoints, the start, and the goal more than once if necessary.
* You can assume that parameters satisfy following conditions.
* 1 <= width <= 100
* 1 <= height <= 100
* The maximum number of checkpoints is 18.

几个样例:


<Input>
5 4
#####
#...#
#S#G#
#####
<Output>
4

<Input>
5 5
#####
#.@@#
#S###
#..G#
#####
<Output>
9

<Input>
5 5
#####
#S..#
##G##
#..@#
#####
<Output>
6

Mean: 

M

 

analyse:

A

Time complexity: O(n)

 

Source code: 

 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-05-21-23.40
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;

const int maxn = 1e2 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
std::vector<int>path;
const int INF = 1 << 20;
struct Point
{
    int x, y;
    bool operator < ( const Point &a )const
    {
        return x < a.x || ( x == a.x ) && y < a.y;
    }
};
std::vector<Point>P;
char mat[maxn][maxn];
int vis[maxn][maxn];
int w, h, s, e;
int d[1 << 20][20];
int dx[] = { -1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int dist[25][25];
int main() {
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );
    while ( cin >> w >> h ) {
        map<Point, int>id;
        P.clear();
        path.clear();
        memset( d, 100, sizeof d );
        memset( dist, 100, sizeof dist );
        for ( int i = 0; i < h; i++ ) {
            scanf( "%s", mat[i] );
            for ( int j = 0; mat[i][j]; ++j ) {
                char &c = mat[i][j];
                if ( c == 'S' || c == 'G' || c == '@' ) {
                    P.pb( ( Point ) {i, j} );
                    int sz = P.size();
                    id[P[sz - 1]] = sz;
                    if ( c == 'S' ) { s = sz - 1; }
                    else if ( c == 'G' ) { e = sz - 1; }
                    path.pb( sz - 1 );
                }
            }
        }
        for ( int i = 0; i < path.size(); i++ ) {
            Point now = P[path[i]];
            int x = path[i];
            //out<<"x "<<x<<endl;
            dist[x][x] = 0;
            memset( vis, 0, sizeof vis );
            vis[now.x][now.y] = 1;
            queue<Point>q;
            q.push( now );
            //cout<<"Bfs"<<endl;
            while ( !q.empty() ) {
                now = q.front(); q.pop();
                for ( int i = 0; i < 4; i++ ) {
                    int nx = now.x + dx[i], ny = now.y + dy[i];
                    if ( nx >= 0 && nx < h && ny >= 0 && ny < w && mat[nx][ny] != '#' && !vis[nx][ny] ) {
                        Point tp = ( Point ) {nx, ny};
                        q.push( tp );
                        vis[nx][ny] = vis[now.x][now.y] + 1;
                        if ( id[tp] ) {
                            dist[x][id[tp] - 1] = vis[now.x][now.y];
                            //cout<<"dist "<<x<<" to "<<id[tp]-1<<' '<<dist[x][id[tp]-1]<<endl;
                        }
                    }
                }
            }
        }
        d[1 << s][s] = 0;
        int M = path.size();
        for ( int i = 0; i < ( 1 << M ); ++i ) {
            for ( int j = 0; j < M; j++ ) {
                int p = path[j];
                for ( int k = 0; 1 << k <= i; k++ ) {
                    if ( i & ( 1 << k ) ) {
                        d[i | ( 1 << p )][p] = min( d[i | ( 1 << p )][p], d[i][k] + dist[k][p] );
                    }
                }
            }
        }
        cout << d[( 1 << M ) - 1][e] << endl;
    }
    return 0;
}
View Code

 

 

 

目录
相关文章
逛街【 腾讯2020校园招聘-后台&综合-第一次笔试】(单调栈的应用)
逛街【 腾讯2020校园招聘-后台&综合-第一次笔试】(单调栈的应用)
68 0
|
算法 程序员
关于校园招聘你必须了解的五件事
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a724888/article/details/81841765 本文首发于我的个人公众号:程序员江湖     欢迎大家关注我的微信公众号:程序员江湖 努力成为最有影响力的程序员自媒体,专注于面试,职场,个人提升三大主题。
|
算法 C++
搜狗2016校园招聘之算法编程解析
1、第一个题:最近邻居 题目: 解题思路: 1)这个题如果用java,相对会好解一些,因为可以直接用JDK中的Point2D类,来定义坐标系空间中的一个点。 2)简单思路:暴力破解,计算任意两个点之间的距离,时间负责度为O(n^2); 3)优化思路:《编程之美》上给出了一个思路,利用分治法,将所有点所在的平面切成点数大致相同的两半,分为Left,Right,则距离最近的两个点,要么在Left区域中,要么在Right区域中,要么在跨两者中间的区域中,时间复杂度可以优化到O(nlgn)。
897 0
|
机器学习/深度学习 缓存 人工智能