0418總結

紅黑樹

#include<iostream>
#include<string.h>
#include <stdio.h>
using namespace std;
struct Node
{
    int left;
    int right;
}node[12];

void In_order(int root)
{
    if(root>=0)
    {
        In_order(node[root].left);
		cout<<root<<endl;
        In_order(node[root].right);
    }
}

int main()
{
    int t1,t2,i,m,n,t,root;
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
        {
			cin>>root>>t1>>t2;
            node[root].left=t1;
            node[root].right=t2;
        }
        scanf("%d",&m);
        for(i=0;i<m;++i)
            scanf("%d%d",&t1,&t2);
        In_order(0);
        cout<<endl;
    }
    return 0;
}


水池數目

import java.io.InputStreamReader;
import java.util.Scanner;

public class nyoj27水池數2014_1_4_10_40_搜索 {

	/**
	 * @param args,由一個節點上下左右的方向進行試探,途中進行標記已經訪問過的點
	 */
	static int map[][];
	static boolean vis[][];
	static int n,m,sum;
	public static int bfs(int i,int j)
	{

		  if(i>n||j>m||i<1||j<1)
			  return sum;		
		 
		if(map[i-1][j]==1&&!vis[i-1][j]){//上
			vis[i-1][j]=true;
			bfs(i-1,j);
		}
		if(map[i][j-1]==1&&!vis[i][j-1]){//左
			vis[i][j-1]=true;
			bfs(i,j-1);
		}
		if(map[i][j+1]==1&&!vis[i][j+1]){//右
			vis[i][j+1]=true;
			bfs(i,j+1);
		}
		if(map[i+1][j]==1&&!vis[i+1][j]){//下
			vis[i+1][j]=true;
			bfs(i+1,j);
		}			

		return sum;	
}
	public static void main(String[] args) {
		 
		Scanner sc = new Scanner(new InputStreamReader(System.in));
		int T = sc.nextInt();
		while(T-->0){
			 n = sc.nextInt();
			 m = sc.nextInt();
			
			map = new int[n+2][m+2];
			vis = new boolean[n+2][m+2];
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++)
					map[i][j]=sc.nextInt();
							
			sum = 0;
			for(int i=1;i<=n;i++)
				for(int j=1;j<=m;j++){
					 if(vis[i][j]||map[i][j]==0)
						 continue;
					 vis[i][j]=true;
					bfs(i,j);
					sum++;
				}
					
			System.out.println(sum);
		}		
	}
}

#include 

#define N 102
#define M 102

int map[N][M];

void search(int i,int j)
{
    if(map[i][j-1]) { map[i][j-1]=0; search(i,j-1); }
    if(map[i][j+1]) { map[i][j+1]=0; search(i,j+1); }
    if(map[i-1][j]) { map[i-1][j]=0; search(i-1,j); }
    if(map[i+1][j]) { map[i+1][j]=0; search(i+1,j); }
}

int main()
{
    int t,n,m;
    int i,j,count;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m); count=0;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&map[i][j]);

        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                if(map[i][j])   { count++; map[i][j]=0; search(i,j); }
        printf("%d\n",count);
    }
    return 0;
}

三個水杯(字典樹)

#include 
#include 
#include 

using namespace std;

#define EMPTY    0

struct data_type
{
	int state[3];
	int step;
};

int cupCapacity[3], targetState[3];

bool visited[100][100][100];

bool AchieveTargetState(data_type current)
{
	for (int i = 0; i < 3; i++)
	{
		if (current.state[i] != targetState[i])
		{
			return false;
		}
	}
	return true;
}

void PourWater(int destination, int source, data_type &cup)
{
	int waterYield = cupCapacity[destination] - cup.state[destination];
	if (cup.state[source] >= waterYield)
	{
		cup.state[destination] += waterYield;
		cup.state[source] -= waterYield;
	}
	else
	{
		cup.state[destination] += cup.state[source];
		cup.state[source] = 0;
	}
}

int BFS(void)
{
	int i, j, k;
	data_type initial;
	queue toExpandState;

	memset(visited, false, sizeof(visited));
	initial.state[0] = cupCapacity[0];
	initial.state[1] = initial.state[2] = 0;
	initial.step = 0;
	toExpandState.push(initial);
	visited[initial.state[0]][0][0] = true;

	while (!toExpandState.empty())
	{
		data_type node = toExpandState.front();
		toExpandState.pop();
		if (AchieveTargetState(node))
		{
			return node.step;
		}
		for (i = 0; i < 3; i++)
		{
			for (j = 1; j < 3; j++)
			{
				k = (i+j)%3;
				if (node.state[i] != EMPTY && node.state[k] < cupCapacity[k])
				{
					data_type newNode = node;
					PourWater(k, i, newNode);
					newNode.step = node.step + 1;
					if (!visited[newNode.state[0]][newNode.state[1]][newNode.state[2]])
					{
						visited[newNode.state[0]][newNode.state[1]][newNode.state[2]] = true;
						toExpandState.push(newNode);
					}
				}
			}
		}
	}
	return -1;
}

int main(void)
{
	int testNum;
	scanf("%d", &testNum);
	while (testNum -- != 0)
	{
		scanf("%d%d%d", &cupCapacity[0], &cupCapacity[1], &cupCapacity[2]);
		scanf("%d%d%d", &targetState[0], &targetState[1], &targetState[2]);
		printf("%d\n", BFS());
	}
	return 0;
}
#include
#include
#include
#include
#include
using namespace std;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
bitset<1000000> Hash;
const int MAX_STEP=100000;
int WQ[MAX_STEP][4],Goal[3],Cap[3],goalval;
int head=0,tail=0;
void movw(int numfrom,int numto,int other)
{
	int total=WQ[head][numfrom]+WQ[head][numto];
	WQ[tail][other]=WQ[head][other];
	WQ[tail][3]=WQ[head][3]+1;
	if(total>Cap[numto])
	{
		WQ[tail][numfrom]=total-Cap[numto];
		WQ[tail][numto]=Cap[numto];
	}
	else
	{
		WQ[tail][numfrom]=0;
		WQ[tail][numto]=total;
	}
	int hashval=WQ[tail][0]*10000+WQ[tail][1]*100+WQ[tail][2];
	if(hashval==goalval) throw WQ[head][3]+1;
	if(WQ[head][numfrom]!=0 && !Hash[hashval]) 
	{
		Hash[hashval]=true;
		if(++tail==MAX_STEP) tail=0;
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		Hash.reset();
		scanf("%d%d%d%d%d%d",&Cap[0],&Cap[1],&Cap[2],&Goal[0],&Goal[1],&Goal[2]);
		head=0,tail=0,goalval=Goal[0]*10000+Goal[1]*100+Goal[2];
		if(Goal[1]==0 && Goal[2]==0 && Goal[0]==Cap[0]) {puts("0");continue;}
		WQ[tail][0]=Cap[0];WQ[tail][1]=0;WQ[tail][2]=0;WQ[tail][3]=0;
		++tail;
		try{
			while(head!=tail)
			{
				movw(0,1,2);movw(1,2,0);movw(0,2,1);movw(1,0,2);movw(2,1,0);movw(2,0,1);
				if(++head==MAX_STEP) head=0;
			}
			puts("-1");
		}catch(int step)
		{
			printf("%d\n",step);
		}
	}
}
/*
第一個問題:
有三個杯子A,B,C,那麼就有六種倒法
A-B A-C
B-A B-C
C-A C-B
如A-B,A把水倒進B中,設A爲杯中已有的體積,A’爲杯中剩餘的體積,A”爲杯子的總體積
這個題倒水能倒就兩種可能:
1:A中的水小於B中的剩餘體積,就全倒進去,A=0,B+=A;
2:A中的水大於B中的剩餘體積,那麼就把B加滿,A=A-B‘,B=B“;
第二個問題:
如何保持程序的有窮性
即如何標記是否之前已走過這一步。
在此建立字典樹判斷是否已存在
比如
6 3 1
4 1 1
案例中出現的情況建立字典樹
(6 0 0)(3 3 0)(3 2 1)(5 0 1)(5 1 0)(4 1 1)
                                               root
                                           /   /    \    \
                                        /     |       |     \
                                      /       /       |       \
                                   /          |        |         \
                                /           /          |            \
                            6           3             5              4
                        /              /  \         /  \            /   \
                      0              3     2      0     1          1     2
                     /              /        \    |         \       /      \
                   0              0           1   1            0    1        0
通過Find函數從根節點開始對比查找是否存在過此情況
*/
#include
#include
#include
using namespace std;

int dir[6][2]={1,2,1,3,2,1,2,3,3,1,3,2};//六種倒法
int v1[3],v11[3];

struct point
{
    int v[3],step;
}p,t;
/*************字典樹的建立跟查找******************/
struct node//字典樹
{
    node* child[105];
};

void Build(node *p,struct point &a)//建樹
{
    int i,j,len;
    len=3;
    for(i=0;ichild[a.v[i]]==NULL)
        {
            node *t=new node;
            for(j=0;j<105;j++)
                t->child[j]=NULL;
            p->child[a.v[i]]=t;
        }
        p=p->child[a.v[i]];
    }
}

int Find(node *p,struct point &a)//查找
{
    int i,len;
    len=3;
    for(i=0;ichild[a.v[i]]==NULL)
            return 1;
        p=p->child[a.v[i]];
    }
    return 0;
}
/**************************************************/

int bfs()//廣搜
{
    int i,x,y,x1;
    node *root=new node;
    for(i=0;i<105;i++)
        root->child[i]=NULL;
    queue q;
    t.v[0]=v1[0];
    t.v[1]=0;
    t.v[2]=0;
    t.step=0;
    Build(root,t);
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t.v[0]==v11[0] && t.v[1]==v11[1] && t.v[2]==v11[2])
            return t.step;
        for(i=0;i<6;i++)
        {
            p=t;
            p.step++;
            x=dir[i][0]-1;
            y=dir[i][1]-1;
            if(t.v[x]>0 && t.v[y]=x1)
                {
                    p.v[x]=t.v[x]-x1;
                    p.v[y]=v1[y];
                }
                else
                {
                    p.v[x]=0;
                    p.v[y]=t.v[y]+t.v[x];
                }
                /***********************************/
                if(Find(root,p))//查找此情況是否出現過
                {
                    Build(root,p);//沒出現過就此情況建樹
                     q.push(p);//入隊列
                }
            }
        }
    }
    return -1;
}

int main()
{
    //freopen("a.txt","w",stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&v1[0],&v1[1],&v1[2]);
        scanf("%d %d %d",&v11[0],&v11[1],&v11[2]);
        printf("%d\n",bfs());
    }
    return 0;
}
#include 
#include 
#include 
#include 

using namespace std;

int va, vb, vc, ta, tb, tc;
bool visit[205][205][205];
struct state
{
    int a;
    int b;
    int c;
    int step;
};

inline bool check(state t)
{
    if(t.a == ta && t.b == tb && t.c == tc)
        return true;
    return false;
}

int BFS(int sa, int sb, int sc)
{
    queue  q;
    state t1, t2;
    t1.a = sa;
    t1.b = sb;
    t1.c = sc;
    t1.step = 0;
    q.push(t1);
    while(!q.empty())
    {
        t1 = q.front();
        q.pop();
        if(t1.b != vb && t1.a)               //  pour va to vb
        {
            if(t1.a <= vb - t1.b)
            {
                t2.a = 0;
                t2.b = t1.b + t1.a;
                t2.c = t1.c;
            }
            else
            {
                t2.a = t1.a - (vb - t1.b);
                t2.b = vb;
                t2.c = t1.c;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
        if(t1.c != vc && t1.a)               //  pour va to vc
        {
            if(t1.a <= vc - t1.c)
            {
                t2.a = 0;
                t2.b = t1.b;
                t2.c = t1.c + t1.a;
            }
            else
            {
                t2.a = t1.a - (vc - t1.c);
                t2.b = t1.b;
                t2.c = vc;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
        if(t1.a != va && t1.b)               //  pour vb to va
        {
            if(t1.b <= va - t1.a)
            {
                t2.b = 0;
                t2.a = t1.b + t1.a;
                t2.c = t1.c;
            }
            else
            {
                t2.b = t1.b - (va - t1.a);
                t2.a = va;
                t2.c = t1.c;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
        if(t1.a != va && t1.b)               //  pour vb to vc
        {
            if(t1.b <= vc - t1.c)
            {
                t2.b = 0;
                t2.a = t1.a;
                t2.c = t1.b + t1.c;
            }
            else
            {
                t2.b = t1.b - (vc - t1.c);
                t2.a = t1.a;
                t2.c = vc;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
        if(t1.a != va && t1.c)               //  pour vc to va
        {
            if(t1.c <= va - t1.a)
            {
                t2.c = 0;
                t2.a = t1.a + t1.c;
                t2.b = t1.b ;
            }
            else
            {
                t2.c = t1.c - (va - t1.a);
                t2.a = va;
                t2.b = t1.b;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
        if(t1.b != vb && t1.c)               //  pour vc to vb
        {
            if(t1.c <= vb - t1.b)
            {
                t2.c = 0;
                t2.b = t1.b + t1.c;
                t2.a = t1.a ;
            }
            else
            {
                t2.c = t1.c - (vb - t1.b);
                t2.b = vb;
                t2.a = t1.a;
            }
            if(!visit[t2.a][t2.b][t2.c])
            {
                t2.step = t1.step + 1;
                if(check(t2))
                    return t2.step;
                visit[t2.a][t2.b][t2.c] = true;
                q.push(t2);
            }
        }
    }
    return -1;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(visit, false, sizeof(visit));
        scanf("%d %d %d", &va, &vb, &vc);
        scanf("%d %d %d", &ta, &tb, &tc);
        if(va == ta && 0 == tb && 0 == tc)
        {
            printf("0\n");
            continue;
        }
        visit[va][0][0] = true;
        printf("%d\n", BFS(va, 0, 0));
    }
    //system("pause");
    return 0;
}


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