暑假訓練第10天,簡單的搜索,POJ 1426,POJ 3984。

題目 POJ 1426

輸入一個小於n的數之後給一個輸出一個十進制只有10組成並且能整除n。

我用的BFS 從從1開始,

然後除以n,

然後遍歷10,11,實質就是1*10的兩個

再遍歷(101,100),(110,111),

。。。。。。。

每次遍歷的數都是2,這就是個2叉樹,

下面是代碼

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
void bfs(int n)
{
    queue<long long>q;
    q.push(1);
    while(!q.empty())
    {
        int i;
        long long x;
        x=q.front();
        q.pop();
        if(x%n==0)
        {
            printf("%lld\n",x);
            return ;
        }
        q.push(x*10);
        q.push(x*10+1);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        bfs(n);
    }
    return 0;
}

 

終於終於我弄懂了一個迷宮輸出,崩潰了2天了,POJ 3984

好不容易會了一點點,一點點!!!BFS,然後他竟然讓我輸出路徑,我emmmmm。。。。

題目讓從左上角走到右下角,然後輸出路徑,一天前我是實現的,需要走幾步

#include<queue>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char map[7][7];
int vis[7][7];
int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; //下上,右左 
struct Node{
	int x,y;
	int step;
};
queue<Node> q;
int bfs(){
	int i;
	Node a,next;
	
	a.x=0;
	a.y=0;
	a.step=0;
	vis[0][0]=1;
	q.push(a);
	while(!q.empty()){
		a=q.front();
		q.pop();
		if(a.x==4&&a.y==4)
		return a.step;
		for(i=0;i<4;i++){
			next.x=a.x+to[i][0];
			next.y=a.y+to[i][1];
			if(next.x<0||next.y<0||next.x>5||next.y>5||next.x=='1'||next.y=='1'||vis[next.x][next.y]==1)
			continue;
			
			vis[next.x][next.y]=1;
			next.step=a.step+1;
			q.push(next);
		}
	}
	return 0; 
}
int main(){
	memset(map,0,sizeof(map));
	memset(vis,0,sizeof(vis));
	for(int i=0;i<5;i++)
	for(int j=0;j<5;j++)
	cin>>map[i][j];
	int ans;
	ans=bfs();
	if(ans)
	cout<<ans<<endl;
	else
	cout<<"NO Way"<<endl;
		
	return 0;
}

 

之後我終於找到了一個大佬的代碼,下面是我對他的代碼的理解


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
 
int Map[6][6],vis[6][6],pre[110];
//pre[]記錄每個狀態的前一個狀態
struct Cam
{
    int x,y;
}List[110];
 
int Dire[4][2] = {-1,0,1,0,0,-1,0,1};   //上下左右四個方向
 
int Go(int x,int y) //判斷是否可走
{
    if(x >= 0 && x < 5 && y >= 0 && y < 5 && Map[x][y] == 0)
        return 1;
    return 0;
}
 
void Print(int x)   //留下寶藏路線
{
    int t;
    t = pre[x];
    if(t == 0)
    {
        printf("(0, 0)\n");
        printf("(%d, %d)\n",List[x].x,List[x].y);
        return ;
    }
    else//遞歸輸出大法,意思就是說你找到寶藏也是不容易的,進入遞歸然後一層進(進入上一個位置),在一層一層進,(直到t==0開始輸入),(從(0,0)開始返回)再一層一層輸出
        Print(t);
    printf("(%d, %d)\n",List[x].x,List[x].y);
}
 
void BFS()
{
    memset(vis,0,sizeof(vis));
    int Head = 0,Tail = 1;
    List[0].x = 0;
    List[0].y = 0;
    pre[0] = -1;//這個pre設置的非常巧妙,直接解決了如何準確找上一步的操作(在下佩服,希望大家下去好好理解下怎麼實現,爲什麼需要這個pre,很重要,是這個題的難點)
    while(Head < Tail)  
    {
        int x = List[Head].x;//更新目前的位置
        int y = List[Head].y;
        if(x == 4 && y == 4)
        {
            Print(Head);//我找到了海賊王留下的寶藏
            return ;
        }
        for(int i = 0; i < 4; ++i)
        {
            int xx = x + Dire[i][0];//遍歷4個位置
            int yy = y + Dire[i][1];
            if( !vis[xx][yy] && Go(xx,yy) )//能走並且沒有走過
            {
                vis[xx][yy] = 1;//標記
                List[Tail].x = xx;//把這個點給標記下存下來,爲了輸出路徑
                List[Tail].y = yy;
                pre[Tail] = Head;//標記前一個位置,就是你現在位置是Tail,上一個位置是Head,與你標記的路徑是匹配輸出的
                Tail++;爲了更新pre
            }
        }
        Head++;
    }
    return ;
}
 
int main()
{
    for(int i = 0; i < 5; ++i)
        for(int j = 0; j < 5; ++j)
            scanf("%d",&Map[i][j]);//建造迷宮
    BFS();//開始搜索寶藏哈哈哈
    return 0;
}

 

好吧下面我需要把自己的改進一下,變成自己的靈活運用 come on!!!

 

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int map[7][7];
int vis[7][7];
int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct none{
	int x;
	int y;
	int pre;
}List[100];

int go(int x,int y){
	if(x>=0&&y>=0&&x<5&&y<5&&map[x][y]==0)
	return 1;
	
	return 0;
}

int couts(int i){
	if(List[i].pre!=-1)
	{
		couts(List[i].pre);
		cout<<"("<<List[i].x<<", "<<List[i].y<<")"<<endl;
	}
}
void bfs(){

	int rear=1,front=0;
	List[0].x=0;
	List[0].y=0;
	List[0].pre=-1;
	vis[0][0]=1;

	while(front<rear)
	{
		for(int i=0;i<4;i++)
		{
			int xx=List[front].x+to[i][0];
			int yy=List[front].y+to[i][1];
			if(vis[xx][yy]==0&&go(xx,yy)!=0)
			{
				vis[xx][yy]=1;
				List[rear].x=xx;
				List[rear].y=yy;
				List[rear].pre=front;
				rear++;
			}
			if(xx==4&&yy==4)
			{
				couts(front);
			}
		}
		front++;
	}
}
int main (){
	for(int i=0;i<5;i++)
	for(int j=0;j<5;j++)
	cin>>map[i][j];
	memset(vis,0,sizeof(vis));
	cout<<"(0, 0)"<<endl;
	bfs();
	cout<<"(4, 4)"<<endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章