}fs[205]; intmain() { int n, a, b, temp; cin >> n >> a >> b; for (int i = 1; i <= n; i++) { cin >> temp; fs[i].up = i + temp; fs[i].down = i - temp; fs[i].step = -1; fs[i].visit = false; } //预处理 queue<floors>q; fs[a].step = 0; //0开始 fs[a].visit = true; q.push(fs[a]); while (!q.empty()) { floors f = q.front(); q.pop(); int u = f.up; int d = f.down; if (u >= 1 && u <= n && fs[u].visit == false) { fs[u].visit = true; fs[u].step = f.step + 1; q.push(fs[u]); } //向上 if (d >= 1 && d <= n && fs[d].visit == false) { fs[d].visit = true; fs[d].step = f.step + 1; q.push(fs[d]); } //向下 if (u == b || d == b) { break; } //到达 } cout << fs[b].step; }
Bessie hears that an extraordinary meteor shower is coming; reports
say that these meteors will crash into earth and destroy anything they
hit. Anxious for her safety, she vows to find her way to a safe location
(one that is never destroyed by a meteor) . She is currently grazing at
the origin in the coordinate plane and wants to move to a new, safer
location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with
meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at
time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it
strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first
quadrant and parallel to the axes at the rate of one distance unit per
second to any of the (often 4) adjacent rectilinear points that are not
yet destroyed by a meteor. She cannot be located on a point at any time
greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe
place.
输入格式
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi,
Yi, and Ti
输出格式
* Line 1: The minimum time it takes Bessie to get to a safe place or
-1 if it is impossible.
#include<iostream> #include<algorithm> #include<string> #include<math.h> usingnamespace std; int n, k; int a[100000]; longlong ans = 0; boolisprime(int a){ if (a == 1) { return0; } for (int i = 2; i * i <= a; i++) { if (a % i == 0) returnfalse; } returntrue; } voiddfs(int m,int sum,int starx) { if (m==k) { if (isprime(sum)) { ans++; } return; } for (int i = starx; i < n; i++) { dfs(m + 1, sum + a[i], i + 1) } } intmain() { cin >> n; cin >> k; for (int i = 0; i <n ; i++) { cin >> a[i]; } dfs(0, 0, 0); cout << ans; return0; }
第 \(2\) 行到第 \(N+1\) 行:每行 \(M\) 个字符,每个字符是 W 或
.,它们表示网格图中的一排。字符之间没有空格。
输出一行,表示水坑的数量。
题目描述
Due to recent rains, water has pooled in various places in Farmer
John's field, which is represented by a rectangle of N x M (1 <= N
<= 100; 1 <= M <= 100) squares. Each square contains either
water ('W') or dry land ('.'). Farmer John would like to figure out how
many ponds have formed in his field. A pond is a connected set of
squares with water in them, where a square is considered adjacent to all
eight of its neighbors. Given a diagram of Farmer John's field,
determine how many ponds he has.
输入格式
Line 1: Two space-separated integers: N and M * Lines 2..N+1: M
characters per line representing one row of Farmer John's field. Each
character is either 'W' or '.'. The characters do not have spaces
between them.
输出格式
Line 1: The number of ponds in Farmer John's field.
#include<iostream> #include<algorithm> #include<string> #include<queue> usingnamespace std; char land[105][105]; bool mark[105][105] = { 0 }; int n, m, ans; structpoint { int x, y; }; int dx[8] = { 0,-1,-1,-1,0,1,1,1 }; int dy[8] = { -1,-1,0,1,1,1,0,-1 }; intmain() { queue<point>q; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> land[i][j]; }
} for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (land[i][j] == 'W' && mark[i][j] == 0) { ans++; mark[i][j] = 1; q.push(point{i, j}); while (!q.empty()) { point p = q.front(); q.pop(); for (int k = 0; k < 8; k++) { int x = p.x + dx[k]; int y = p.y + dy[k]; if (x<0||y<0) { continue; } if (land[x][y]=='W'&&mark[x][y]==0) { mark[x][y] = 1; q.push(point{ x,y }); } } } } } } cout << ans; }
#include<iostream> #include<queue> usingnamespace std; int land[35][35] = { 0 }; int mark[35][35] = { 0 }; int n, tmp; int dx [4] = {-1,1,0,0}; int dy[4] = {0,0,-1,1}; structpoint { int x, y;
}; queue<point>q; intmain() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) {
cin >> tmp; if (tmp) { land[i][j] = 1; } } } mark[0][0] = 1; q.push(point{ 0, 0 }); while (!q.empty()) { point p = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int x = p.x + dx[i]; int y = p.y + dy[i]; if (x<0 || x>n + 1 || y<0 || y>n + 1) { continue; } if (land[x][y] == 0 && mark[x][y] == 0) { mark[x][y] = 1; q.push(point{ x,y });
This past fall, Farmer John took the cows to visit a corn maze. But
this wasn't just any corn maze: it featured several gravity-powered
teleporter slides, which cause cows to teleport instantly from one point
in the maze to another. The slides work in both directions: a cow can
slide from the slide's start to the end instantly, or from the end to
the start. If a cow steps on a space that hosts either end of a slide,
she must use the slide.
The outside of the corn maze is entirely corn except for a single
exit.
The maze can be represented by an N x M (2 <= N <= 300; 2 <=
M <= 300) grid. Each grid element contains one of these items:
* Corn (corn grid elements are impassable)
* Grass (easy to pass through!)
* A slide endpoint (which will transport a cow to the other
endpoint)
* The exit
A cow can only move from one space to the next if they are adjacent
and neither contains corn. Each grassy space has four potential
neighbors to which a cow can travel. It takes 1 unit of time to move
from a grassy space to an adjacent space; it takes 0 units of time to
move from one slide endpoint to the other.
Corn-filled spaces are denoted with an octothorpe (#). Grassy spaces
are denoted with a period (.). Pairs of slide endpoints are denoted with
the same uppercase letter (A-Z), and no two different slides have
endpoints denoted with the same letter. The exit is denoted with the
equals sign (=).
Bessie got lost. She knows where she is on the grid, and marked her
current grassy space with the 'at' symbol (@). What is the minimum time
she needs to move to the exit space?