蓝桥训练4

一、关联矩阵  

问题描述

  有一个n个结点m条边的有向图,请输出他的关联矩阵。

输入格式

  第一行两个整数n、m,表示图中结点和边的数目。n<=100,m<=1000。
  接下来m行,每行两个整数a、b,表示图中有(a,b)边。
  注意图中可能含有重边,但不会有自环。

输出格式

  输出该图的关联矩阵,注意请勿改变边和结点的顺序。

样例输入

5 9
1 2
3 1
1 5
2 5
2 3
2 3
3 2
4 3
5 4

样例输出

1 -1 1 0 0 0 0 0 0
-1 0 0 1 1 1 -1 0 0
0 1 0 0 -1 -1 1 -1 0
0 0 0 0 0 0 0 1 -1
0 0 -1 -1 0 0 0 0 1


思路:邻接矩阵表示点和边之间的关系, 对于有向图来说,有向边的起始点为1,有向边的终点为-1, 其他为0;

C++:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int n, m;
int a, b;
int mapp[1000][1000];
int main()
{
    scanf("%d%d",&n, &m);
    memset(mapp, 0, sizeof(mapp));
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d", &a, &b);
        mapp[a][i] = 1;
        mapp[b][i] = -1;
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1;j<=m;j++)
        {
            printf("%d ",mapp[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Java:

import java.util.Scanner;
public class Main 
{
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int a, b;
		int n = cin.nextInt();
		int m = cin.nextInt();
		int[][]mapp = new int [n+1][m+1];
		for(int i=1; i<=m; i++)
		{
			a = cin.nextInt();
			b = cin.nextInt();
			mapp[a][i] = 1;
			mapp[b][i] = -1;
		}
		for(int i=1; i<=n; i++)
		{
			for(int j=1; j<=m; j++)
			{
				System.out.print(mapp[i][j]+" ");
			}
			System.out.println();
		}
	}
}

最后输出,一开始是这样写的,但是不过 求大佬指点:

for(int i=1; i<=n; i++)
{
    for(int j=1; j<=m; j++)
    {
	System.out.printf("%d ",mapp[i][j]);
    }
    System.out.println();
}

二、前缀表达式  

问题描述

  编写一个程序,以字符串方式输入一个前缀表达式,然后计算它的值。输入格式为:“运算符 对象1 对象2”,其中,运算符为“+”(加法)、“-”(减法)、“*”(乘法)或“/”(除法),运算对象为不超过10的整数,它们之间用一个空格隔开。要求:对于加、减、乘、除这四种运算,分别设计相应的函数来实现。
  输入格式:输入只有一行,即一个前缀表达式字符串。
  输出格式:输出相应的计算结果(如果是除法,直接采用c语言的“/”运算符,结果为整数)。
  输入输出样例

样例输入

+ 5 2

样例输出

7

C++:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
char str;
int x1, x2;
int main()
{
    scanf("%c%d%d",&str, &x1, &x2);
    if(str=='+')
        cout<<x1+x2;
    else if(str=='-')
        cout<<x1-x2;
    else if(str=='*')
        cout<<x1*x2;
    else
        cout<<x1/x2;
    return 0;
}

Java:

import java.util.Scanner;
public class Main 
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		String s = cin.next();
		char str = s.charAt(0);
		int x1 = cin.nextInt();
		int x2 = cin.nextInt();
		if(str=='+')
			System.out.println(x1+x2);
		else if(str == '-')
			System.out.println(x1-x2);
		else if(str == '*')
			System.out.println(x1*x2);
		else if(str == '/')
			System.out.println(x1/x2);
	}			
}

三、学霸的迷宫  

问题描述

  学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。

输入格式

  第一行两个整数n, m,为迷宫的长宽。
  接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。

输出格式

  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。

样例输入

Input Sample 1:
3 3
001
100
110

Input Sample 2:
3 3
000
000
000

样例输出

Output Sample 1:
4
RDRD

Output Sample 2:
4
DDRR

数据规模和约定

  有20%的数据满足:1<=n,m<=10
  有50%的数据满足:1<=n,m<=50
  有100%的数据满足:1<=n,m<=500。

C++:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int mapp[510][510];
int dex[4] = {-1, 1, 0, 0};
int dey[4] = {0, 0, -1, 1};
int n, m;
struct node
{
    int x, y, sum;
    char str;
}s[510][510];
int bfs()
{
    int i;
    queue<node>q;
    node f;
    node g;
    f.x = f.y = f.sum = 0;
    mapp[f.x][f.y] = 1;
    q.push(f);
    while(!q.empty())
    {
        f = q.front();
        q.pop();
        if (f.x==n-1&&f.y==m-1)
        {
            return f.sum;
        }
        for(int i=0; i<4; i++)
        {
            g.x = f.x+dex[i];
            g.y = f.y+dey[i];
            if(g.x>=0&&g.y>=0&&g.x<n&&g.y<m&&mapp[g.x][g.y]==0)
            {
                g.sum=f.sum+1;
                s[g.x][g.y].x=f.x;
                s[g.x][g.y].y=f.y;
                if (i==0)
                    s[g.x][g.y].str='U';
                else if (i==1)
                    s[g.x][g.y].str='D';
                else if (i==2)
                    s[g.x][g.y].str='L';
                else
                    s[g.x][g.y].str='R';
                mapp[g.x][g.y]=1;
                q.push(g);
            }
        }
    }
}
void solve(int xx,int yy)
{
	if (xx==0&&yy==0)
	 {
	 	return ;
	 }
	 solve(s[xx][yy].x, s[xx][yy].y);
	 printf("%c",s[xx][yy].str);
}
int main()
{
	int t;
	scanf("%d%d",&n,&m);
	for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
            scanf("%1d",&mapp[i][j]);
    }
    t = bfs();
    printf("%d\n",t);
	solve(n-1,m-1);
	return 0;
}

Java:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Main 
{
	public static char [][]ch=new char[505][505];
	public static int n,m;
	public static int [][]num;
	public static int [][]dis={{1,0},{0,-1},{0,1},{-1,0}};
	public static int [][]vis=new int [505][505];
	public static int [][]ss=new int [20][20];
	public static class node
	{
		int x,y,step;
		node(){};
		StringBuffer r=new StringBuffer();//相当于类里面的一个变量,一开始定义为空;
	}
	static  node t=new node();
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner cin=new Scanner (System.in);
		n=cin.nextInt();
		m=cin.nextInt();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				vis[i][j]=0;
			}
		}
		for(int i=0;i<n;i++)
		{
			ch[i]=cin.next().toCharArray();	//直接输入二维数组;
		}
		System.out.println(bfs(0,0));
		System.out.println(t.r);
	}
	private static int bfs(int x, int y) 
	{
		// TODO Auto-generated method stub
		vis[x][y]=1;
		Queue<node>q=new LinkedList<node>();
		node s = new node();
		s.x = x;
		s.y = y;
		s.step = 0;
		q.add(s);
		while(!q.isEmpty())
		{
			s=q.peek();
			q.poll();
			if(s.x==n-1&&s.y==m-1)
			{
				t.r=s.r;
				return s.step;
			}
				
			for(int i=0;i<4;i++)
			{
				node e=new node();
				e.x=s.x+dis[i][0];
				e.y=s.y+dis[i][1];
				if(e.x<0||e.x>=n||e.y<0||e.y>=m)
					continue;
				if(vis[e.x][e.y]==1)
					continue;
				if(ch[e.x][e.y]!='0')
					continue;
				e.step=s.step+1;
				vis[e.x][e.y]=1;
				e.r.append(s.r);//把s里面的字符重新加到e里面;
				if(i==0)e.r.append("D");//把路径记录到结构体里面的r;
				if(i==1)e.r.append("L");
				if(i==2)e.r.append("R");
				if(i==3)e.r.append("U");	
				q.add(e);	
			}
		}
		return 0;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42569807/article/details/88218592