首页 > 试题广场 >

小美的新游戏

[编程题]小美的新游戏
  • 热度指数:2448 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

小美和小团合作开发了一款新游戏!他们相信这款游戏一定可以大火。

游戏规则是这样的,现在有一个方格地图,你控制一个机器人位于初始位置(x, y),然后你可以向上下左右的地块移动。其中一些地块上会有得分点,经过这些点可以获得分数。当然,路上还会有一些陷阱点,如果想要通过陷阱点,就需要付出一定的分数来清除这个陷阱点。注意陷阱点付出分数清除后就会变成普通的地块。即反复经过陷阱点只需付出一次代价。同样的,得分点也只能获得一次分数。

 

小美想到了一个策划案来让这个游戏变得难一些。小美把地图和机器人的初始位置给了小团,并且告诉了小团他操控机器人的行进路线。小美想试试小团能不能算出来他的最终得分。

小团完美地完成了这个任务。现在,小美和小团想找一些测试人员看看这款游戏的难度如何。他们找到了你,希望你帮他们测试一下这个游戏。而你能否挑战成功呢?

 

注意分数允许为负。初始分数为0.


输入描述:

第一行四个整数N,M,P,Q,表示这张地图是N行M列的,得分点的得分是P,陷阱点清除的代价是Q。

接下来N行,每行M个字符,表示这张地图。其中,字符S表示初始机器人位置。字符#表示墙壁,字符O代表得分点。字符X代表陷阱点。字符+代表普通的地块。

 

接下来一行一个连续的字符串表示机器人的移动路线,只由大写字母WASD构成,W向上,A向左,S向下,D向右。机器人可以上下左右移动。不能超出地图边界。也不能走到墙壁之上。试图走出边界和走到墙壁的行动会停留在原来的位置不动。

 



输出描述:

一个整数,表示小团的机器人最终获得了多少分

示例1

输入

6 6 20 10
S#++O#
OXX#X#
++++++
###XX#
++#O#+
OXO++X
SSDDDDDAWWSSSAWSSSADDD

输出

40

备注:

其中地图大小对全体数据都有1<=N,M<=500

对于得分点分数和陷阱点分数P和Q对全体数据都有1<=P,Q<=1000

接下来是机器人行进路线,对于30%的测试点,有行进路线长度小于等于100;对于100%的测试点有行进路线长度小于等于100000

直接模拟机器人的移动过程,遇到墙壁不移动,遇到陷阱点和得分点后对分数进行增量,并将陷阱点和得分点都改成普通地块。
def move(direction):
    global x, y, score, grid
    if direction == 'A':
        if y > 0 and grid[x][y - 1] != '#':
            if grid[x][y - 1] == 'O':
                # 如果是得分点就进行得分,并且经过了的得分点变为普通地块
                score += P
            elif grid[x][y - 1] == 'X':
                # 如果是陷阱点,就付出代价将其变为普通地块,然后到达
                score -= Q
            y -= 1
            grid[x][y] = '+'
    elif direction == 'S':
        if x < N - 1 and grid[x + 1][y] != '#':
            if grid[x + 1][y] == 'O':
                score += P
            elif grid[x + 1][y] == 'X':
                score -= Q
            x += 1
            grid[x][y] = '+'
    elif direction == 'W':
        if x > 0 and grid[x - 1][y] != '#':
            if grid[x - 1][y] == 'O':
                score += P
            elif grid[x - 1][y] == 'X':
                score -= Q
            x -= 1
            grid[x][y] = '+'
    elif direction == 'D':
        if y < M - 1 and grid[x][y + 1] != '#':
            if grid[x][y + 1] == 'O':
                score += P
            elif grid[x][y + 1] == 'X':
                score -= Q
            y += 1
            grid[x][y] = '+'

if __name__ == "__main__":
    N, M, P, Q = map(int, input().strip().split())
    grid = []
    x, y = 0, 0
    for i in range(N):
        grid.append(list(input().strip()))
        for j in range(M):
            if grid[i][j] == 'S':
                x, y = i, j
    path = input().strip()
    score = 0
    for i in range(len(path)):
        move(path[i])
    print(score)


发表于 2021-03-05 14:33:02 回复(1)
简洁代码混一波
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int p=sc.nextInt();
        int q=sc.nextInt();
        sc.nextLine();
        char[][] chars=new char[n][m];
        int curi=0,curj=0;
        for (int i = 0; i < n; i++) {
            chars[i]=sc.nextLine().toCharArray();
            for (int j = 0; j < m; j++) {
                if (chars[i][j]=='S'){
                    curi=i;
                    curj=j;
                }
            }
        }

        String str = sc.nextLine();
        Map<Character,int[]> map=new HashMap<>();
        map.put('W',new int[]{-1,0});
        map.put('A',new int[]{0,-1});
        map.put('S',new int[]{1,0});
        map.put('D',new int[]{0,1});
        int res=0;
        for (int i = 0; i < str.length(); i++) {
            int ni=curi+map.get(str.charAt(i))[0];
            int nj=curj+map.get(str.charAt(i))[1];
            if (ni>=n||ni<0||nj>=m||nj<0||chars[ni][nj]=='#'){
                ni=curi;
                nj=curj;
            }
            if (chars[ni][nj]=='O'){
                res+=p;
                chars[ni][nj]='+';
            }
            if (chars[ni][nj]=='X'){
                res-=q;
                chars[ni][nj]='+';
            }
            curi=ni;
            curj=nj;
        }
        System.out.println(res);
    }
}


发表于 2021-06-01 16:16:11 回复(0)
import java.util.*;
public class Main{
    public static int total = 0; 
    static int score = 0;
    static int bar = 0;
    public static void main(String[]args){
        
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int line = sc.nextInt();
        score = sc.nextInt();
        bar = sc.nextInt();
        sc.nextLine();
        
        char [][] map = new char[row][line];
        int sr=0;
        int sl=0;
        //初始化地图
        for(int r= 0; r<row; r++){
            char [] siline = sc.nextLine().toCharArray();
            for (int l = 0; l<line ; l++){
                //确定开始位置
                if(siline[l]=='S'){
                    sr = r;
                    sl = l;
                }
                map[r][l] = siline[l];
            }
        }
        
        char [] orders = sc.nextLine().toCharArray();
       
         
        int r = sr;
        int l = sl;
        for(char s :orders){
            //得到下一步的走向
             switch (s){
               case 'W':
                     if(r-1>=0 && map[r-1][l]!='#'){
                           r = r-1;
                     }
                 
                break;
            case 'A':
                     if(l-1>=0&& map[r][l-1]!='#'){
                       l = l-1;
                     }
         
            break;
            case 'S':
                     if(r+1<=map.length-1&& map[r+1][l]!='#'){
                          r = r+1; 
                     }
              
            break;
            case 'D':
                     if(l+1<=map[0].length-1&& map[r][l+1]!='#'){
                          l = l+1; 
                     }
            break;   
         }
        // 算出得分
        switch (map[r][l]){
            case 'O':
                total = total + score;
                map[r][l] = '+';
                break;
            case 'X':
                total = total - bar;
                map[r][l] = '+';
            break;    
        }
        }
        System.out.println(total);
    }
 
}

发表于 2021-03-12 19:31:58 回复(1)
忘记把起点变成'+'了 真是笨蛋!!
发表于 2021-04-13 00:18:07 回复(2)
java
package travers.mt;

import java.util.Scanner;

public class mt21_7_t2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        int q = sc.nextInt();
        char[][] map = new char[n][m];
        for (int i = 0; i < n; i++){
            String s = sc.next();
            map[i] = s.toCharArray();
        }
        boolean[][] visited = new boolean[n][m];
        String g = sc.next();
        int len = g.length();
        int x = -1, y = -1;
        for (int i = 0; i < n; i ++){
            for (int j = 0; j < m; j++){
                if (map[i][j] == 'S'){
                    x = i;
                    y = j;
                    break;
                }
            }
        }
        int res = 0;
        char[] go = g.toCharArray();
        for (int i = 0; i < len; i++){
            if (go[i] == 'S' && x + 1 < n && map[x+1][y] != '#'){
                if (map[x+1][y] == 'O' && !visited[x+1][y]){
                    res += p;
                }else if (map[x+1][y] == 'X' && !visited[x+1][y]) {
                    res -= q;
                }
                visited[x+1][y] = true;
                x+=1;
            }else if (go[i] == 'W' && x - 1 >= 0 && map[x-1][y] != '#'){
                if (map[x-1][y] == 'O' && !visited[x-1][y]){
                    res += p;
                }else if (map[x-1][y] == 'X' && !visited[x-1][y]) {
                    res -= q;
                }
                visited[x-1][y] = true;
                x-=1;
            }else if (go[i] == 'A' && y - 1 >= 0 && map[x][y-1] != '#'){
                if (map[x][y-1] == 'O' && !visited[x][y-1]){
                    res += p;
                }else if (map[x][y-1] == 'X' && !visited[x][y-1]) {
                    res -= q;
                }
                visited[x][y-1] = true;
                y-=1;
            }else if (go[i] == 'D' && y + 1 < m && map[x][y+1] != '#'){
                if (map[x][y+1] == 'O' && !visited[x][y+1]){
                    res += p;
                }else if (map[x][y+1] == 'X' && !visited[x][y+1]) {
                    res -= q;
                }
                visited[x][y+1] = true;
                y+=1;
            }
        }
        System.out.println(res);
    }
}



发表于 2023-03-29 20:33:05 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
 
using namespace std;
 
int main()
{
    int rows, cols, points, trap, ret = 0, x, y;
    cin >> rows >> cols >> points >> trap;
    // 字符S表示初始机器人位置。字符#表示墙壁,字符O代表得分点。字符X代表陷阱点。字符+代表普通的地块。
    vector<vector<char>> vec(rows, vector<char>(cols));
    for (int i = 0; i < rows; ++i)
        for (int j = 0; j < cols; ++j)
        {
            cin >> vec[i][j];
            if(vec[i][j] == 'S')
                x = i, y = j;
        }
 
    string route;
    cin >> route;
    // W向上,A向左,S向下,D向右。机器人可以上下左右移动。不能超出地图边界。也不能走到墙壁之上。
    // 试图走出边界和走到墙壁的行动会停留在原来的位置不动
    for (const char c : route)
    {
        switch(c)
        {
            case 'W':{
                if (x - 1 < 0 || vec[x - 1][y] == '#')
                    continue;
                --x;
                break;
            }
            case 'A':{
                if (y - 1 < 0 || vec[x][y - 1] == '#')
                    continue;
                --y;
                break;
            }
            case 'S':{
                if (x + 1 == rows || vec[x + 1][y] == '#')
                    continue;
                ++x;
                break;
            }
            case 'D':{
                if (y + 1 == cols || vec[x][y + 1] == '#')
                    continue;
                ++y;
                break;
            }
        }
 
        if(vec[x][y] == 'O')
                ret += points;
        else if (vec[x][y] == 'X')
            ret -= trap;
             
        vec[x][y] = '+';
    }
 
    cout << ret;
}

发表于 2023-03-14 13:56:21 回复(0)
def creat_map(N=3, M=2):
'''创建地图的函数,分别包含以下默认参数:
行数 N
列数 M
'''
map_list = []
l = 0
while(l < N):
r = 0
line = []
line_str = input()
while r < M:
line.append(line_str[r])
r = r+1
map_list.append(line)
l = l+1

return map_list

pass

def play_game(N, M, P, Q, game_map, move):

# 搜索S初始位置
x = 0 # 行
y = 0 # 列
score = 0
for i in range(N):
for j in range(M):
if(game_map[i][j] == 'S'):
x = i
y = j
game_map[i][j] = '+'
break

# 开始从S点移动
for i in range(len(move)):
# 向上移动
if(move[i] == 'W'):
if(x > 0 and game_map[x-1][y] != '#'): # 成功移动
x = x-1
if(game_map[x][y] == 'O'):
score += P # 加分
game_map[x][y] = '+' # 变为空地
elif(game_map[x][y] == 'X'):
score -= Q # 扣分
game_map[x][y] = '+' # 变为空地
# 向下移动
elif(move[i] == 'S'):
if(x < N-1 and game_map[x+1][y] != '#'):
x = x+1
if(game_map[x][y] == 'O'):
score += P # 加分
game_map[x][y] = '+' # 变为空地
elif(game_map[x][y] == 'X'):
score -= Q # 扣分
game_map[x][y] = '+' # 变为空地
# 向左移动
elif(move[i] == 'A'):
if(y > 0 and game_map[x][y-1] != '#'):
y = y-1
if(game_map[x][y] == 'O'):
score += P # 加分
game_map[x][y] = '+' # 变为空地
elif(game_map[x][y] == 'X'):
score -= Q # 扣分
game_map[x][y] = '+' # 变为空地
# 向右移动
elif(move[i] == 'D'):
if(y < M-1 and game_map[x][y+1] != '#'):
y = y+1
if(game_map[x][y] == 'O'):
score += P # 加分
game_map[x][y] = '+' # 变为空地
elif(game_map[x][y] == 'X'):
score -= Q # 扣分
game_map[x][y] = '+' # 变为空地

print(score) # 输出结果

pass

str_nmpq = input()
str_nmpq_list = str_nmpq.split()
N = int(str_nmpq_list[0])
M = int(str_nmpq_list[1])
P = int(str_nmpq_list[2])
Q = int(str_nmpq_list[3])

game_map = creat_map(N, M)
move = input()
play_game(N, M, P, Q, game_map, move)
''' 测试地图
6 6 20 10
S#++O#
OXX#X#
++++++
###XX#
++#O#+
OXO++X
SSDDDDDAWWSSSAWSSSADDD
'''

发表于 2023-02-08 14:59:08 回复(0)
import java.util.Scanner;

/**
 * @Author: LI
 * @Date: Created in 15:57 2022/8/26
 */
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.doTheWork();
    }


    int N, M, P, Q, X, Y, count;
    char[][] chars;

    private void doTheWork() {
        Scanner sc = new Scanner(System.in);
        String[] line1 = sc.nextLine().split(" ");
        N = Integer.parseInt(line1[0]);
        M = Integer.parseInt(line1[1]);
        P = Integer.parseInt(line1[2]);
        Q = Integer.parseInt(line1[3]);
        chars = new char[N][M];
        count = 0;
        for (int i = 0; i < N; i++) {
            char[] charArray = sc.nextLine().toCharArray();
            for (int j = 0; j < M; j++) {
                chars[i][j] = charArray[j];
                if (charArray[j] == 'S') {
                    X = i;
                    Y = j;
                }
            }
        }
        char[] act = sc.nextLine().toCharArray();
        for (char c : act) {
            if (c == 'W') move(X - 1, Y);
            else if (c == 'A') move(X, Y - 1);
            else if (c == 'S') move(X + 1, Y);
            else move(X, Y + 1);
        }
        System.out.println(count);
    }

    private void move(int a, int b) {
        if (a >= N || a < 0 || b >= M || b < 0 || chars[a][b] == '#') {

        } else if (chars[a][b] == 'O') {
            count += P;
            chars[a][b] = '+';
            X = a;
            Y = b;
        } else if (chars[a][b] == 'X') {
            count -= Q;
            chars[a][b] = '+';
            X = a;
            Y = b;
        } else {
            X = a;
            Y = b;
        }
    }
}

发表于 2022-08-26 19:53:24 回复(0)
const [n, m, p, q] = readline().split(' ').map(Number)
const map = new Array(n).fill(0)
for(let i = 0; i < n; i++) {
    map[i] = readline().split('')
}
const op = readline()
const point = {
    x: 0,
    y: 0
}
for(let i = 0; i < n; i++) {
    for(let j = 0; j < m; j++) {
        if(map[i][j] == 'S') {
            point.x = i
            point.y = j
            break;
        }
    }
}

let score = 0
for(let i = 0; i < op.length; i++) {
    let [x, y] = [point.x, point.y] // 临时保存以便恢复
    switch(op[i]) {
        case 'W':
            point.x -= 1
            break
        case 'S':
            point.x += 1
            break
        case 'A':
            point.y -= 1
            break
        case 'D':
            point.y += 1
            break
    }
    if(point.x < 0 || point.x >= n || point.y < 0 || point.y >= m) { // 如果越界了
        point.x = x
        point.y = y
    }
    switch(map[point.x][point.y]) {
        case 'O':
            score += p
            map[point.x][point.y] = '+'
            break
        case 'X':
            score -= q
            map[point.x][point.y] = '+'
            break
        case '#': // 撞墙了
            point.x = x
            point.y = y
            break
        case '+':
            break
    }
}
console.log(score)

发表于 2022-06-06 21:24:03 回复(0)
直接模拟,ban取一个与0和P和-Q不同的值即可,没给输入范围就按照输入的值动态设置。
#include<iostream>;
#include<algorithm>;
using namespace std;
bool searchone(int n);
int main(){
    int N,M,P,Q;
    int glography[15000][15000];
    int start[2];
    int ban= -9999;
    char in;
    long score;
    cin >> N >> M >> P>>Q;
    for(int i =0;i<N;i++){
        for(int j=0;j<M;j++){
            cin >>in;
            if(in=='S'){
                start[0]=i;
                start[1]=j;
            }else if(in=='#'){
                glography[i][j]=ban;
            }else if(in=='O'){
                glography[i][j]=P;
            }else if(in=='X'){
                glography[i][j]=-Q;
            }
        }
    }
    while(cin>>in){
        if(in=='W'&&start[0]>0&&glography[start[0]-1][start[1]]!=ban){
            start[0]--;
        }else if(in=='A'&&start[1]>0&&glography[start[0]][start[1]-1]!=ban){
            start[1]--;
        }else if(in=='S'&&start[0]<N-1&&glography[start[0]+1][start[1]]!=ban){
            start[0]++;
        }else if(in=='D'&&start[1]<M-1&&glography[start[0]][start[1]+1]!=ban){
            start[1]++;
        }else{
            continue;
        }
        score = score+glography[start[0]][start[1]];
        glography[start[0]][start[1]]=0;
    }
    cout << score;
}
发表于 2022-05-31 11:48:27 回复(0)

还是要提升做题速度才行啊,太慢了
package main

import "fmt"

func Game(){
	var row,col,P,Q int
	var order string
	fmt.Scan(&row,&col,&P,&Q)
	flag:=make([][]int,row)
	GameMap:=make([]string,row)
	for i:=0;i<row;i++{
		fmt.Scan(&GameMap[i])
	}
	fmt.Scan(&order)
	t1,t2:=0,0
	score:=0
	//找到起始位置
	for i:=0;i<row;i++{
		flag[i]=make([]int,col)
		for j:=0;j<col;j++{
			if GameMap[i][j]=='S'{
				t1=i
				t2=j
			}
		}
	}
	//循环指令
	for i:=0;i<len(order);i++{
		switch order[i] {
		case 'W':
			if t1 > 0 {
				t1--
			if GameMap[t1][t2]=='#'{
				t1++
			}else {
				Choose(GameMap[t1][t2],&score,P,Q,t1,t2,flag)

			}
			}
		case 'S':
			if t1 < row-1 {
				t1++
				if GameMap[t1][t2]=='#' {
					t1--
				}else {
					Choose(GameMap[t1][t2],&score,P,Q,t1,t2,flag)
				}
			}
		case 'A':
			if t2>0{
				t2--
				if GameMap[t1][t2] == '#' {
					t2++
				}else {
					Choose(GameMap[t1][t2],&score,P,Q,t1,t2,flag)

				}
			}
		case 'D':
			if t2<col-1{
				t2++
				if GameMap[t1][t2]=='#' {
					t2--
				}else {
					Choose(GameMap[t1][t2],&score,P,Q,t1,t2,flag)

				}
			}
		}
	}
	fmt.Println(score)

}

func Choose(a uint8,score *int,P,Q int,row,col int,flag [][]int)  {
	switch a {
	case 'O':
		if flag[row][col]==0 {
			*score+=P
			flag[row][col]=1
		}else {
			return
		}
	case 'X':
		if flag[row][col]==0 {
			*score-=Q
			flag[row][col]=1
		}else {
			return
		}
	case '+':
		return
	}
}

func main() {
	Game()
}


发表于 2022-05-26 16:09:14 回复(0)
按题意模拟即可。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: LemonHuang
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] strs1 = br.readLine().trim().split(" ");
        int N = Integer.parseInt(strs1[0]);
        int M = Integer.parseInt(strs1[1]);
        int P = Integer.parseInt(strs1[2]);
        int Q = Integer.parseInt(strs1[3]);
        char[][] map = new char[N][M];
        for(int i = 0; i < N; i++){
            String temp = br.readLine().trim();
            for(int j = 0; j < M; j++){
                map[i][j] = temp.charAt(j);
            }
        }
        String s = br.readLine().trim();
        char[] ops = s.toCharArray();
        //记录起点坐标
        int x = 0, y = 0;
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++){
                if(map[i][j] == 'S'){
                    x = i;
                    y = j;
                }
            }
        }
        int res = 0;
        for(int i = 0; i < ops.length; i++){
            char op = ops[i];
            if(op == 'W'){
                x--;
                if(x < 0 || map[x][y] == '#'){
                    x++;
                    continue;
                }
            }else if(op == 'S'){
                x++;
                if(x >= N || map[x][y] == '#'){
                    x--;
                    continue;
                }
            }else if(op == 'A'){
                y--;
                if(y < 0 || map[x][y] == '#'){
                    y++;
                    continue;
                }
            }else if(op == 'D'){
                y++;
                if(y >= M || map[x][y] == '#'){
                    y--;
                    continue;
                }
            }
            //计算得分并更新地块
            if(map[x][y] == 'O'){
                res += P;
                map[x][y] = '+';
            }
            if(map[x][y] == 'X'){
                res -= Q;
                map[x][y] = '+';
            }
        }
        System.out.println(res);
    }
}


发表于 2022-03-19 18:52:27 回复(1)
阅读理解题
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int p = Integer.parseInt(s[2]); // 得分
        int q = Integer.parseInt(s[3]); // 扣分
        char[][] chess = new char[n][m]; // 棋盘
        int x = 0;
        int y = 0;
        int score = 0;
        for (int i = 0; i < n; i++) {
            String str = reader.readLine();
            for (int j = 0; j < m; j++) {
                if(str.charAt(j) == 'S'){
                    x = i;
                    y = j;
                }
                chess[i][j] = str.charAt(j);
            }
        }
        String path = reader.readLine();
        char[] route = new char[path.length()];// 路线
        for (int i = 0; i < path.length(); i++) {
            route[i] = path.charAt(i);
        }
        for (int i = 0; i < route.length; i++) {
            if(route[i] == 'W' && x-1>=0 && chess[x-1][y]!='#'){
                x--;
            }else if(route[i] == 'S' && x+1<n && chess[x+1][y]!='#'){
                x++;
            }else if(route[i] == 'A' && y-1>=0 && chess[x][y-1]!='#'){
                y--;
            }else if(route[i] == 'D' && y+1<m && chess[x][y+1]!='#'){
                y++;
            }
            if(chess[x][y] == 'O'){
                score+=p;
                chess[x][y] = '+';
            }else if(chess[x][y] == 'X'){
                score-=q;
                chess[x][y] = '+';
            }
        }
        System.out.println(score);
    }
}



发表于 2022-03-03 21:36:49 回复(0)
模拟一下就可以了
/**
 *      author:刷完这题就去睡觉
 *      created:
**/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void add(int x,int y,int &ans,vector<vector<char>>&grid,int a,int b){
    if(grid[x][y]=='O'){
        ans+=a;
        grid[x][y]='+';
    }
    if(grid[x][y]=='X'){
        ans-=b;
        grid[x][y]='+';
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,p,q;
    cin>>n>>m>>p>>q;
    vector<vector<char>>map;
    pair<int,int>start={0,0};
    for(int i=0;i<n;i++){
        map.push_back(vector<char>());
        for(int j=0;j<m;j++){
            char a;
            cin>>a;
            if(a=='S')start={i,j};
            map[i].push_back(a);
        }
    }
    int ans=0;
    string s;
    cin>>s;
    queue<pair<int,int>>qq;
    qq.push(start);
    for(int i=0;i<s.size();i++){
        auto node=qq.front();
        int row=node.first,col=node.second;
        if(s[i]=='W'){
            if(row-1<0||map[row-1][col]=='#')continue;
            else{
                qq.push({row-1,col});
                add(row-1,col,ans,map,p,q);
                qq.pop();
            }
        }
        if(s[i]=='A'){
            if(col-1<0||map[row][col-1]=='#')continue;
            else{
                qq.push({row,col-1});
                add(row,col-1,ans,map,p,q);
                qq.pop();
            }
        }if(s[i]=='S'){
            if(row+1>=n||map[row+1][col]=='#')continue;
            else{
                qq.push({row+1,col});
                add(row+1,col,ans,map,p,q);
                qq.pop();
            }
        }if(s[i]=='D'){
            if(col+1>=m||map[row][col+1]=='#')continue;
            else{
                qq.push({row,col+1});
                add(row,col+1,ans,map,p,q);
                qq.pop();
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

发表于 2021-08-16 19:45:13 回复(0)
一个思想:将一个字母的地图转化成int类型的二维数租,S机器人的位置数组值为0,#为1,O得分点为正值,X陷阱点为负值,+普通地块为0.
这样将走过地方的数值相加,最后就是结果了
再用一个数组进行实时标记机器人的位置。
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int M = sc.nextInt();
    int P = sc.nextInt();
    int Q = sc.nextInt();
    int a[][] = new int[N][M];
    int op[] = new int[2];//位置  行、列
    int scope = 0;
    for (int i = 0; i < a.length; i++) {
      String ss=sc.next();
      for (int j = 0; j < ss.length(); j++) {
        char c = ss.charAt(j);
        if (c == 'S') {
          a[i][j] = 0;
          op[0] = i;//行
          op[1] = j;//列
        }
        if (c == '#') {
          a[i][j] = 1;
        }
        if (c == 'O') {
          a[i][j] = P;
        }
        if (c == 'X') {
          a[i][j] = -Q;
        }
        if (c == '+') {
          a[i][j] = 0;
        }
      }
    }
    String s = sc.next();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (c == 'W') {
        if (op[0] - 1 >= 0) {
          if (a[op[0] - 1][op[1]] != 1) {
            scope += a[op[0] - 1][op[1]];
            op[0] = op[0] - 1;
            a[op[0]][op[1]]=0;
          }
        }
      }
      if (c == 'A') {
        if (op[1] - 1 >= 0) {
          if (a[op[0]][op[1] - 1] != 1) {
            scope += a[op[0]][op[1] - 1];
            op[1] = op[1] - 1;
            a[op[0]][op[1]]=0;
          }
        }
      }
      if (c == 'S') {
        if (op[0] + 1 < a.length) {
          if (a[op[0] + 1][op[1]] != 1) {
            scope += a[op[0] + 1][op[1]];
            op[0] = op[0] + 1;
            a[op[0] ][op[1]]=0;
          }
        }
      }
      if (c == 'D') {
        if (op[1] + 1 < a[0].length) {
          if (a[op[0]][op[1] + 1] != 1) {
            scope += a[op[0]][op[1] + 1];
            op[1] = op[1] + 1;
            a[op[0]][op[1]]=0;
          }
        }
      }
    }
    System.out.println(scope);
  }
}


发表于 2021-08-15 00:21:37 回复(0)
n, m, p, q = list(map(int, input().split()))
mp = []
for i in range(n):
    tmp = list(input())
    if 'S' in tmp:
        location = [i, tmp.index('S')]
    mp.append(tmp)
ans = 0
mv = list(input())
for ch in mv:
    if ch == 'W':
        new_i, new_j = location[0] - 1, location[1]
    elif ch == 'S':
        new_i, new_j = location[0] + 1, location[1]
    elif ch == 'A':
        new_i, new_j = location[0], location[1] - 1
    elif ch == 'D':
        new_i, new_j = location[0], location[1] + 1
    if 0 <= new_i < n and 0 <= new_j < m:
        if mp[new_i][new_j] == '#':
            continue
        if mp[new_i][new_j] == 'O':
            ans += p
            mp[new_i][new_j] = '+'
            location = [new_i, new_j]
        elif mp[new_i][new_j] == 'X':
            ans -= q
            mp[new_i][new_j] = '+'
            location = [new_i, new_j]
        else:
            location = [new_i, new_j]
print(ans)


发表于 2021-07-09 17:11:25 回复(0)
50%,不知道哪里出问题了,
思路一样,先判断边界,然后判断有没有动,然后判断是不是墙,如果是墙回退,最后判断奖励和陷阱
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() {     int n,m,p,q;     cin>>n>>m>>p>>q;     vector<vector<char> > maps(10000, vector<char>(10000, 'd'));     int x,y;     for(int i=0;i<n;i++){         string str;         cin>>str;         for(int j=0;j<m;j++){             maps[i][j]=str[j];             if(str[j]=='S'){                 x=i;                 y=j;             }         }     }     string road;     int result=0;     cin>>road;     for(int i=0;i<road.size();i++){                  int x1=x,y1=y;         if(road[i]=='S'){             if(x+1<m){                 x+=1;             }         }         else if(road[i]=='W'){             if(x-1>-1){                 x-=1;             }         }         else if(road[i]=='A'){             if(y-1>-1){                 y-=1;             }         }         else if(road[i]=='D'){             if(y+1<n){                 y+=1;             }         }                           if(x1==x && y1==y)             continue;         if(maps[x][y]=='#'){             x=x1;             y=y1;             continue;         }                  if(maps[x][y]=='O'){             result+=p;             maps[x][y]='+';         }         else if(maps[x][y]=='X'){             result-=q;             maps[x][y]='+';         }     }     cout<<result<<endl;     return 0; }

编辑于 2021-05-06 22:41:28 回复(0)
直接模拟,走过的陷阱和得分砖改为普通砖,判断是不是墙壁,如果是就不动
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int P = sc.nextInt();
        int Q = sc.nextInt();
        char[][] map = new char[N][M];
        for(int i = 0;i<N;i++){
            String tmp = sc.next();
            char[] column = tmp.toCharArray();
            map[i] = column;
        }
        String route = sc.next();
        print(P,Q,map,route);
    }
    public static void print(int P,int Q,char[][] map,String route){
        int x = 0,y = 0;
        for(int i = 0;i<map.length;i++){
            for(int j = 0;j<map[0].length;j++){
                if(map[i][j]=='S'){
                    x=i;
                    y=j;
                }
            }
        }
        int point = 0;
        int getPoint = 0;
        for(int i = 0;i<route.length();i++){
            if(route.charAt(i)=='W'){
                if(x-1<0){
                    continue;
                }else{
                    getPoint=point(map,x-1,y,P,Q);
                    if(getPoint!=-1){
                        point+=getPoint;
                        x=x-1;
                    }
                }
            }
            if(route.charAt(i)=='S'){
                if(x+1>map.length-1){
                    continue;
                }else{
                    getPoint=point(map,x+1,y,P,Q);
                    if(getPoint!=-1){
                        point+=getPoint;
                        x=x+1;
                    }
                }
            }
            if(route.charAt(i)=='A'){
                if(y-1<0){
                    continue;
                }else{
                    
                    getPoint=point(map,x,y-1,P,Q);
                    if(getPoint!=-1){
                        point+=getPoint;
                        y=y-1;
                    }
                }
            }
            if(route.charAt(i)=='D'){
                if(y+1>map[0].length-1){
                    continue;
                }else{
                    getPoint=point(map,x,y+1,P,Q);
                    if(getPoint!=-1){
                        point+=getPoint;
                        y=y+1;
                    }
                }
            }
        }
        System.out.print(point);
    }
    public static int point(char[][] map,int x,int y,int P,int Q ){
        if(map[x][y]=='O'){
            map[x][y]='+';
            return P;
        } 
        if(map[x][y]=='X'){
            map[x][y]='+';
            return -1*Q;
        } 
        if(map[x][y]=='#'){
            return -1;
        } 
        return 0;
    }
}


发表于 2021-05-05 20:52:18 回复(0)
只通过了50%,不知道哪里错了
def isOver(tmp):
    if tmp[0]<0 or tmp[0]>n-1 or tmp[1]<0 or tmp[1]>n-1:
        return False
    else:
        return True
def Score(map,start,pro,sco):
    if pro=='W':
        tmp=[start[0]-1,start[1]]
    elif pro=='A':
        tmp=[start[0],start[1]-1]
    elif pro=='S':
        tmp=[start[0]+1,start[1]]
    else:
        tmp=[start[0],start[1]+1]
    if isOver(tmp):
        if map[tmp[0]][tmp[1]]=='#':
            return [sco,start]
        elif map[tmp[0]][tmp[1]]=='O':
            map[tmp[0]][tmp[1]]='+'
            return [sco+p,tmp]
        elif map[tmp[0]][tmp[1]]=='X':
            map[tmp[0]][tmp[1]]='+'
            return [sco-q,tmp]
        else:
            return [sco,tmp]
    else:
        return [sco,start]

n,m,p,q=map(int,input().split())
map=[[] for _ in range(n)]
start=[-1,-1]
for i in range(n):
    map[i]=list(input())
    if start==[-1,-1] and 'S' in map[i]:
        start=[i,map[i].index('S')]
        map[i][start[1]]='+'

walk=input()
score=0
if len(walk)==0:
    print(0)
    exit()
for item in walk:
    score,start=Score(map,start,item,score)
print(score)


编辑于 2021-04-26 20:56:02 回复(1)