反射计数
华为OD机考 - 华为OD上机考试 200分题型
华为OD机考真题目录点击查看: 华为OD机考真题题库目录|机考题库 + 算法考点详解
题目描述
给定一个包含 0 和 1 的二维矩阵。
给定一个初始位置和速度,一个物体从给定的初始位置出发,在给定的速度下进行移动,遇到矩阵的边缘则发生镜面发射。
无论物体经过 0 还是 1,都不影响其速度。
请计算并给出经过 t 时间单位后,物体经过 1 点的次数。
矩阵以左上角位置为 [0, 0](列(x),行(y)),例如下面A点坐标为 [2, 1](第二列,第一行)
注意:
- 如果初始位置的点是 1,也计算在内
- 时间的最小单位为 1,不考虑小于 1 个时间单位内经过的点
输入描述
第一行为初始信息
w h x y sx sy t
第二行开始一共 h 行,为二维矩阵信息
其中:
- w,h 为矩阵的宽和高
- x,y 为起始位置
- sx,sy 为初始速度
- t 为经过的时间
所有输入都是有效的,数据范围如下:
- 0 < w < 100
- 0 < h < 100
- 0 ≤ x < w
- 0 ≤ y < h
- -1 ≤ sx ≤ 1
- -1 ≤ sy ≤ 1
- 0 ≤ t <100
输出描述
经过 1 的个数,注意初始位置也要计算在内.
用例1
输入
12 7 2 1 1 -1 13 001000010000 001000010000 001000010000 001000010000 001000010000 001000010000 001000010000输出
3说明
初始位置为(2,1),速度为(1,-1),那么13个时间单位后,经过点1的个数为3
题解
思路:逻辑分析
- 这道题主要要分析出两个点
- 由样例和图综合可以得出,x和y于以往不一样,x和y是反的。
- 镜面反射就是当某一个方向触底时,值不变,方向变为相反。例如当
x < 0 || x >= w时,设置sx = -sx
- 明白这个两个点就通过模拟实现就行,当
某个方向坐标触底(两个边界)时,对应方向的速度取相反数。然后统计经过合法坐标1的数量就行。 - 按照2的逻辑处理t秒之后,输出经过
1的数量就行。
c++
#include<iostream> #include<vector> #include<string> #include <utility> #include <sstream> #include<algorithm> #include<cmath> #include<map> using namespace std; int main() { int w,h,x,y,sx,sy,t; cin >> w >> h >> x >> y >> sx >> sy >>t; vector<string> grid(h); for (int i = 0; i < h; i++) { cin >> grid[i]; } int count = 0; while (t >= 0) { // 由图可知,这题x和y是反的,因此y其实是行号,x是列号 if (grid[y][x] == '1') { count++; } y += sy; x += sx; if (x < 0) { x = 1; sx = -sx; } else if (x >= w) { x = w - 2; sx = -sx; } if (y < 0) { y = 1; sy = -sy; } else if (y >= h) { y = h -2; sy = -sy; } t--; } cout << count; return 0; }JAVA
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int sx = sc.nextInt(); int sy = sc.nextInt(); int t = sc.nextInt(); // 读取地图 String[] grid = new String[h]; for (int i = 0; i < h; i++) { grid[i] = sc.next(); } int count = 0; while (t >= 0) { // 由题意可知,这里 x 和 y 是反的 // y 表示行号,x 表示列号 if (grid[y].charAt(x) == '1') { count++; } y += sy; x += sx; // 处理 x 方向反弹 if (x < 0) { x = 1; sx = -sx; } else if (x >= w) { x = w - 2; sx = -sx; } // 处理 y 方向反弹 if (y < 0) { y = 1; sy = -sy; } else if (y >= h) { y = h - 2; sy = -sy; } t--; } System.out.println(count); } }Python
importsys# 读取参数w,h,x,y,sx,sy,t=map(int,sys.stdin.readline().split())# 读取地图grid=[]for_inrange(h):grid.append(sys.stdin.readline().strip())count=0whilet>=0:# 注意:题目中 x 和 y 是反的# y 是行号,x 是列号ifgrid[y][x]=='1':count+=1y+=sy x+=sx# x 方向反弹ifx<0:x=1sx=-sxelifx>=w:x=w-2sx=-sx# y 方向反弹ify<0:y=1sy=-syelify>=h:y=h-2sy=-sy t-=1print(count)JavaScript
constreadline=require('readline');constrl=readline.createInterface({input:process.stdin,output:process.stdout});constlines=[];rl.on('line',line=>{lines.push(line.trim());});rl.on('close',()=>{letidx=0;let[w,h,x,y,sx,sy,t]=lines[idx++].split(' ').map(Number);// 读取地图constgrid=[];for(leti=0;i<h;i++){grid.push(lines[idx++]);}letcount=0;while(t>=0){// 注意:y 是行号,x 是列号if(grid[y][x]==='1'){count++;}y+=sy;x+=sx;// x 方向反弹if(x<0){x=1;sx=-sx;}elseif(x>=w){x=w-2;sx=-sx;}// y 方向反弹if(y<0){y=1;sy=-sy;}elseif(y>=h){y=h-2;sy=-sy;}t--;}console.log(count);});Go
packagemainimport("bufio""fmt""os")funcmain(){in:=bufio.NewReader(os.Stdin)varw,h,x,y,sx,sy,tintfmt.Fscan(in,&w,&h,&x,&y,&sx,&sy,&t)// 读取地图grid:=make([]string,h)fori:=0;i<h;i++{fmt.Fscan(in,&grid[i])}count:=0fort>=0{// 注意:y 是行号,x 是列号ifgrid[y][x]=='1'{count++}y+=sy x+=sx// x 方向反弹ifx<0{x=1sx=-sx}elseifx>=w{x=w-2sx=-sx}// y 方向反弹ify<0{y=1sy=-sy}elseify>=h{y=h-2sy=-sy}t--}fmt.Println(count)}