NOIP蒟蒻的//专栏
一只NOIp的蒟蒻,可能会写以下内容: # 计算机和程序设计 # 题解 # 物理(虽然并没过B卷) # 各种神奇的练习题
文号D907184
1655
0
收藏
编辑文章

洛谷 P1002 [NOIP 2002 普及组] 过河卒 的题解

上海市华二陆家嘴分校,栖霞路34号

中文摘要

我使用2种方法完成了这道题,一种是BFS法一种是DFS法,BFS法优于DFS法 祝大家算法学习顺利,前进!

Abstract

i use bfs and dfs to solve this problem,bfs is better.

关键词

算法算法竞赛C语言洛谷算法题编程计算机科学noip

BFS法题解:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct point { int x; int y; };

struct point horse, horsemid;

struct point end;

struct point pawn;

char count=1;

int main() {

scanf("%d%d%d%d", &end.x, &end.y, &horse.x, &horse.y);

long board[end.x+3][end.y+3];

for (char a=0;a<end.x+3;a++){

for (char b=0;b<end.y+3;b++){

board[a][b]=1;

}

}

horse.x++;

horse.y++;

board[horse.x][horse.y]=0;

board[horse.x+2][horse.y-1]=0;

board[horse.x+2][horse.y+1]=0;

board[horse.x+1][horse.y+2]=0;

board[horse.x+1][horse.y-2]=0;

board[horse.x-1][horse.y-2]=0;

board[horse.x-1][horse.y+2]=0;

board[horse.x-2][horse.y-1]=0;

board[horse.x-2][horse.y+1]=0;

for (char a=0;a<end.x+3;a++){

board[a][0]=0;

board[a][end.y+2]=0;

}

for (char a=0;a<end.y+3;a++){

board[0][a]=0;

board[end.x+2][a]=0;

}

for (char a=1;a<=end.x+1;a++){

for (char b=1;b<=end.y+1;b++){

if(board[a][b]!=0){

board[a][b]=board[a][b-1]+board[a-1][b];

}

if(a==1&&b==1){

board[a][b]=1;

}

}

}

printf("%ld\n",board[end.x+1][end.y+1]);

return 0;

}





DFS法题解:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct point { char x; char y; };

struct point horse, horsemid;

struct point end;

struct point pawn;

long dfs(char (*ptr)[end.y+3],char x,char y ){

static long count =0;

if(ptr[x][y]==10){

count ++;

}

if (ptr[x+1][y]==0||ptr[x+1][y]==10){

dfs(ptr,x+1,y);

}

if(ptr[x][y+1]==0||ptr[x][y+1]==10){

dfs(ptr,x,y+1);

}

return count;

}

int main() {

scanf("%d%d%d%d", &end.x, &end.y, &horse.x, &horse.y);

char board[end.x+3][end.y+3];

for (char a=0;a<end.x+3;a++){

for (char b=0;b<end.y+3;b++){

board[a][b]=0;

}

}

for (char a=0;a<end.x+3;a++){

board[a][0]=1;

board[a][end.y+2]=1;

}

for (char a=0;a<end.y+3;a++){

board[0][a]=1;

board[end.x+2][a]=1;

}

board[end.x+1][end.y+1]=10;

horse.x++;

horse.y++;

board[horse.x][horse.y]=1;

board[horse.x+2][horse.y-1]=1;

board[horse.x+2][horse.y+1]=1;

board[horse.x+1][horse.y+2]=1;

board[horse.x+1][horse.y-2]=1;

board[horse.x-1][horse.y-2]=1;

board[horse.x-1][horse.y+2]=1;

board[horse.x-2][horse.y-1]=1;

board[horse.x-2][horse.y+1]=1;

printf("%ld\n",dfs(board,1,1));

return 0;

}

0
还没有人评论哦~~

想参与大家的讨论?现在就 登录 或者 注册。

主体信息
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:浙江
 内容统计
2025年
5 篇
2023年
2 篇
2022年
3 篇
2021年
2 篇
2020年
5 篇
2019年
2 篇
ID: {{ user.uid }}
投诉或举报
加载中...
{{ tip }}
请选择违规类型:
{{ reason.type }}

空空如也

加载中...
回复
设为精选取消精选
评学术分
鼓励
编辑
查看历史
通过审核
移动文章
加入收藏取消收藏
退修或删除
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{ format('YYYY/MM/DD HH:mm:ss', toc) }}
回复
加入收藏取消收藏