//BFS算法
#include<bits/stdc++.h>
using namespace std;
char mp[30][50]; //地图
bool vis[30][50]; //标记已访问
int dir[4][2] = {{1,0},{0,-1},{0,1},{-1,0}}; //下、左、右、上
char dirc[4] = {'D','L','R','V'};
int n,m; //行、列
struct node{
int x; //横坐标
int y; //纵坐标
int step; //步数
string str; //路径
node(int xx, int yy, int ss, string s){
x = xx;
y = yy;
step = ss;
str = s;
}
}
queue<node> q; //创建队列
bool check(int x, int y){
if(x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || mp[x][y] == '1'){
return false;
}
return true;
}
void bfs(int x, int y){
q.push(node(x,y,0,""));
vis[x][y] = true;
while(!q.empty()){
node now = q.front();
if(now.x == n-1 && now.y == m-1){
cout<<now.str<<endl;
cout<<now.step<<endl;
break;
}
q.pop();
for(int i = 0; i < 4; i++){
int nx = now.x + dir[i][0];
int ny = now.y + dir[i][1];
if(check(nx,ny)){
q.push(node(nx,ny,now.step + 1,now.str + dirc[i]));
vis[nx][ny] = true;
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++){
scanf("%s",mp[i]);
}
bfs(0,0);
return 0;
}
//特别数的和
#include<bits/stdc++.h>
using namespace std;
bool check(int n){
int res = 0;
while(n){
res = n % 10;
if(res == 0 || res == 1 || res == 2 || res == 9){
return false;
}
n = n / 10;
}
return true;
}
int main(){
int n;
scanf("%d",&n);
int ans = 0;
for(int i = 1; i <= n; i++){
if(check(i)){
ans = ans + i;
}
}
cout<<ans<<endl;
return 0;
}
//字符串出现了多少次
#include<bits/stdc++.h>
using namespace std;
char s1[1005], s2[1005];
int main(){
gets(s1);
gets(s2);
int len1 = strlen(s1), len2 = strlen(s2);
int ans = 0;
for(int i = 0; i + len2 - 1 < len1; i++){
bool matched = true;
for(int j = 0; j < len2; j++){
if(s1[i+j] != s2[j]){
matched = false;
break;
}
}
if(matched){
ans++;
}
}
cout<<ans<<endl;
return 0;
}
//判断偶数
#include<bits/stdc++.h>
using namespace std;
char s[1005];
int main(){
int len;
scanf("%s",s);
len = strlen(s);
if((s[len - 1] - '0') % 2 == 0){
printf("YES");
}else{
printf("NO");
}
return 0;
}
//字符串反转
#include<bits/stdc++.h>
using namespace std;
char s[100005];
int main(){
int len;
scanf("%s",s);
len = strlen(s);
for(int i = len - 1; i >= 0; i--){
printf("%c",s[i]);
}
printf("\n");
return 0;
}
//R进制数
#include<bits/stdc++.h>
using namespace std;
char ans[105];
int main(){
int N, R, m, now;
cin>>N>>R;
if(N < 0){
cout<<"-";
N = -N;
}
m = 0;
while(N){
now = N % R;
if(now <= 9){
ans[m++] = '0' + now;
}else{
ans[m++] = 'A' + now - 10;
}
N = N / R;
}
if(m == 0){
cout<<0;
}
for(int i = m - 1; i >= 0; i--){
cout<<ans[i];
}
cout<<endl;
return 0;
}
//回文数字(五位或者六位整数)
#include<bits/stdc++.h>
using namespace std;
int n;
int digit[6];
bool judge(int x){
int m = 0, sum = 0;
while(x){
digit[m++] = x % 10;
sum += x % 10;
x = x / 10;
}
if(sum != n){
return false;
}
for(int i = 0; i < m / 2; i++){
if(digit[i] != digit[m-1-i]){
return false;
}
}
return true;
}
int main(){
bool f = false;
cin>>n;
for(int i = 10000; i < 100000; i++){
if(judge(i)){
cout<<i<<endl;
f = true;
}
}
if(!f){
cout<<-1<<endl;
}
return 0;
}
蓝桥杯复习1
最新推荐文章于 2023-03-28 15:24:07 发布