文章目录Robot area 机器人运动范围思路TagRobot area 机器人运动范围
有一个 m*n的矩阵,从【0,0】到【m-1,n-1】。机器人从【0,0】开始移动,每一次可以上下左右移动一格。不能出界,也不能进入行坐标与列坐标数字之和大于k的格子。计算机器人…
题目来源:PAT (Advanced Level) Practice
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user …
题目来源:PAT (Advanced Level) Practice
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from …
相关题目: 207. 课程表 210. 课程表 II 1462. 课程表 IV
class CourseSchedule:"""207.课程表https://leetcode.cn/problems/course-schedule/"""def __init__(self):# 记录⼀次递归堆栈中的节点self.onPath []# 记录遍历过的节点&…
Pots Time Limit: 1000MS Memory Limit: 65536KBSubmit StatisticProblem Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;DR…
Coach Pang loves his boyfriend Uncle Yang very much. Today is Uncle Yang’s birthday, Coach Pang wants to have a romantic candlelit dinner at Uncle Yang’s house and he has to arrive there in T minutes. There are N houses in their city numbered from 1 …
文章目录打开转盘锁 Open the Lock思路Tag打开转盘锁 Open the Lock
有一个带4个圆形转轮的转盘锁,每个转轮有10个数字 0-9,转轮可以自由转。每次旋转只能转一个转轮的一个数字。
初始数字为0000,一个代表4个转轮数字的字符串。
列表deade…
题目
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has t…
题面 题解(Flood Fill 模型) 用bfs遍历每一个房间,每次遍历返回房间的大小,同时标记这些房间已经被访问过,时间复杂度O(nm) ,每个房间的墙壁信息可以提前用坐标表示,我们设坐标为 int dx[4] {0, -1, 0, 1} ; int dy[4…
[ICPC2015 WF]Keyboarding
题面翻译
给定一个 r r r 行 c c c 列的在电视上的「虚拟键盘」,通过「上,下,左,右,选择」共 5 5 5 个控制键,你可以移动电视屏幕上的光标来打印文本。一开始,光…
1091 Acute Stroke (30point(s))
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume…
文章目录二叉树的堂兄弟 Cousins in Binary Tree思路Tag二叉树的堂兄弟 Cousins in Binary Tree
在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k1 处。
如果二叉树的两个节点深度相同,但 父节点不同 ,则…
题目
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal…
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greate…
题目
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 …
题目
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now and then, t…
剑指 Offer 13. 机器人的运动范围 - 力扣(LeetCode) (leetcode-cn.com) class Solution {int m, n, k;vector<vector<bool>> not_visited;
public:int movingCount(int M, int N, int K) {m M; n N; k K;not_visited vector<vector…
【Q310】(md) 被围绕的区域 给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。 找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。 示例: X X X XX O O X X X O XX O X X 运行你的函数后࿰…
传送门 题意:判断相同字母能否连成环; bfs直接开搞; #include<bits/stdc.h>
using namespace std;
int d[4][2] {0,1,1,0,0,-1,-1,0};//遍历四个方向
int vis[100][100];
char tu[100][100];
int n, m, ans;
void bfs(int x, int y, i…
题目:
Given a binary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level).
For example: Given binary tree [3,9,20,null,null,15,7], 3/ \9 20/ \15 7return its level order traversal as:
[[3],[9…
就是一个搜索,但是好像还是有点问题,第六组样例没过去。。
#include <bits/stdc.h>
#define cl(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
char mp[1500][1500];
int vis[1500][1500];
int n,m,sx,sy,tx,ty;
int dx[] {0,0,1,-…
//存储容器:1:node father[maxn][maxn];//当前节点的父节点
2:struct node//*** { int x,y,d; char pos;//存储D L R U };
具体存储:
for(int i0;i<4;i){int toxnow.xdix[i];int toynow.ydiy[i];father[tox][toy].x…
官网
题目
1090. Highest Price in Supply Chain (25)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of retailers(零售商), distributors(经销商&a…
问题描述如下图所示,3 x 3 的格子中填写了一些整数。--*----|10* 1|52|--****--|20|30* 1|*******--| 1| 2| 3|------我们沿着图中的星号线剪开,得到两个部分,每个部分的数字和都是60。本题的要求就是请你编程判定:对给定的m x n …
目录
Lake Counting S
求细胞数量
海战
组合的输出
div3 A. Square
div3 B. Arranging Cats Lake Counting S
P1596 [USACO10OCT] Lake Counting S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
感谢大佬的指点!!!!
思…
一 、 DFS
#include<iostream>
#include<vector>
#include<cstring>
#define MAX 20005
using namespace std;int m, n;
vector<int> g[MAX];
int max_i 0;
int max_s 0;
int visit[MAX] {0};
void dfs(int cur, int s){if(s > max_s){max_s …
比赛链接
好忙好忙好忙,慢慢补老比赛的题解了。
这场没啥算法,全是思维。有也是BFS,屎。 A. Special Characters
题意:
您将得到一个整数 n n n 。
您的任务是构建一串大写的拉丁字母。此字符串中必须正好有 n n n 个特殊字…
1.全球变暖 - 蓝桥云课 (lanqiao.cn) import os import sys # 请在此输入您的代码 sys.setrecursionlimit(60000) n int(input()) dao [] for _ in range(n): tmp list(input()) dao.append(tmp) dict [(1, 0), (0, 1), (-1, 0), (0, -1)] used [[0 for _ in range(n)] fo…
题目链接
题目链接 https://vjudge.net/problem/POJ-2965
原题目
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in …
题面 题解(Flood Fill 模型) 本题是八联通形式,我们直接枚举每一个格子,如果有水并且是没有被标记过的,那么就开始做Flood Fill ,将所有联通的水源标记,联通的数量1, 代码
#include<iostream>
#incl…
剑指Offer 32 -Ⅱ 从上到下打印二叉树
题目地址
https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/ 代码实现
经典广度搜索问题
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeN…
题目
法1:经典BFS 下图中就展示了我们方法:
class Solution {public int[][] updateMatrix(int[][] mat) {int m mat.length, n mat[0].length;int[][] dist new int[m][n];boolean[][] used new boolean[m][n];Queue<int[]> queue new Li…
到达某个点最少走几步,涉及广度优先搜索(BFS),要用到队列。 做题思路: 由输出的矩阵看出这个马只能按照象棋中的走法跳,由此可以定义一个移动的数组:
int dir[8][2]{{2,1},{1,2},{-1,2},{-2,1}…
5347. 使网格图至少有一条有效路径的最小代价 BFS中每个点只入列一次,但有时,我们可能需要bfs中允许一个点入列多次才能解决问题,这种方法就是SPFA。 class Solution {public int minCost(int[][] grid) {int m grid.length, n grid[0].len…
2.危险系数 - 蓝桥云课 (lanqiao.cn) n, m map(int, input().split())
map_ [[] for i in range(n 1)]
used [0 for i in range(n 1)]
used_ [0 for i in range(n 1)]
cnt 0
res []
for _ in range(m):u, v map(int, input().split())map_[u].append(v)map_[v].appen…
Problem Description我们称一个有向图G是传递的,当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c。我们称图G是一个竞赛图,当且仅当它是一个有向图且它的基图是完全图。换句 话说,将完全图每…
【题目来源】https://www.acwing.com/problem/content/190/
【题目描述】 农民 John 有很多牛,他想交易其中一头被 Don 称为 The Knight 的牛。 这头牛有一个独一无二的超能力,在农场里像 Knight 一样地跳(就是我们熟悉的象棋中马的走法&…
这场没考什么算法,比较水,难度也不是很高。比赛链接
硬要说的话E有个 前缀和 加 二分,F是数学BFS,G是个构造 A. Turtle Puzzle: Rearrange and Negate
题意:
给你一个由 n n n 个整数组成的数组 a a a 。您必须对…
题面 题解 从起点开始,步数为0,然后BFS能走到’日’的方向,更新距离即可 代码
#include<bits/stdc.h>using namespace std;
typedef pair<int, int> PII;
const int N 160;int n, m;
char g[N][N];
PII q[N * N];
int dist[N][…
从前一个和谐的班级,有 nl 个是男生,有 nr 个是女生。编号分别为 1,…,nl 和 1,…,nr。
有若干个这样的条件:第 v 个男生和第 u 个女生愿意结为配偶,且结为配偶后幸福程度为 ww。
请问这个班级里幸福程度之和最大是多少…
奇怪的电梯
题目链接
题目描述
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 i i i 层楼( 1 ≤ i ≤ N 1 \le i \le N 1≤i≤N)上有一个数字 K i K_i Ki( 0…
题目 One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core. Input …
题目:HDU-1026 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid1026 题目: Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15647 Acce…
迷宫的最短路径时间限制:1000 ms | 内存限制:65535 KB难度:3描述:给定一个大小为N * M 的迷宫。迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动。请求出从起点到终点所需的最小步数。请注意,本题假定…
题目大意:给定一张图,包括 . * o x ,分别为可行点,目标(可能不止一个),墙;目标:以最短的距离遍历这个图上的所有 * ,机器人可以直接清理垃圾无需时间;…
BFS(Breadth first search)
适用范围:找到一条最快到达目标状态的路径。
算法过程:将当前搜索到的状态A的每一个子状态压入队列,检查队列是否为空,如果不为空,弹出队首元素,并且以…
题目链接:有向图的拓扑排序
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N 100010;int n, m;
int h[N], e[N], ne[N], idx;int q[N], d[N];void add(int a, int b)
{e[idx] b, ne[idx] h[a]…
L - Grayscale Confusion
题目大意
有 n n n 个三元组 { r i , g i , b i } \{r_i,\space g_i,\space b_i\} {ri, gi, bi},需要构造一个数组 w i w_i wi 使得 w 1 w 2 w_1w_2 w1w2 并且对于 ∀ i , j \forall i,\space j ∀i, j 满足如果 r i &…
Description Ever since the incident on the hill, Jack and Jill dislike each other and wish to remain as distant as possible. Jack and Jill must attend school each day; Jack attends a boys’ school while Jill attends a girls’ school. Both schools start at…
【题目来源】https://www.acwing.com/problem/content/4523/【题目描述】 给定一个正整数 X,请你在 X 后面添加若干位数字(至少添加一位数字;添加的数不能有前导0),使得结果为质数,在这个前提下所得的结果应…