剩余18天………… 练一发搜索 从基础开始吧……

来源:互联网 发布:depaul university知乎 编辑:程序博客网 时间:2024/05/17 02:51

洛谷 p1451
题目描述

一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。(1<=m<=80,1<=n<=50)

输入输出格式

输入格式:
输入:整数m,n(m行,n列)

矩阵

输出格式:
输出:细胞的个数

输入输出样例

输入样例#1:

4 10
0234500067
1034560500
2045600671
0000000089

输出样例#1:

4

#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<string>using namespace std;bool map[100][100];int zuobiao[2][4] = { {1,0,-1,0} , {0,1,0,-1} }; //分别代表下右上左int main(){    int m, n, ans = 0;    scanf("%d%d", &m, &n);    for (int i = 1; i <= m; i++)     {        string a;        cin >> a;//用字符串处理读入        for (int j = 0; j < n; j++)        {            if (a[j] != '0')//转为01矩阵 用bool型节省空间                map[i][j+1] = true;            else map[i][j+1] = false;        }    }    for(int i=1;i<=m;i++)        for (int j = 1; j <= n; j++)        {            if (map[i][j])            {                queue<int> x, y;//广搜基本模型 采用队列形式,这里x代表横坐标 y代表纵坐标                int xz, yz;                ans++;//细胞数++                x.push(i);//横坐标入队                y.push(j);//纵坐标入队                map[i][j] = 0;//如果处理完成就将其变为0 防止回溯                while (!x.empty())                {                    for (int k = 0; k <= 4; k++)                    {                        xz = x.front() + zuobiao[0][k];                        yz = y.front() + zuobiao[1][k];                        if (xz >= 1 && xz <= m&&yz >= 1 && yz <= n&&map[xz][yz])//注意越界!                        {                            x.push(xz);                            y.push(yz);                            map[xz][yz] = false;                        }                    }                    x.pop();//弹出初始的坐标                    y.pop();                }            }        }    cout << ans;    return 0;}

已用 vs2015 编译通过……

洛谷 P1443 马的遍历

题目描述

有一个n*m的棋盘(1< n , m <=200),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步

输入输出格式

输入格式:
一行四个数据,棋盘的大小和马的坐标

输出格式:
一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1)

输入输出样例

输入样例#1:

3 3 1 1
输出样例#1:
0 3 2
3 -1 1
2 1 4

//还是广搜的题……#include<iostream>#include<queue>#include<iomanip>using namespace std;const int maxn = 201;int map[maxn][maxn];int n, m, move_[8][2] = { { 1,2 },{ 1,-2 },{ -1,2 },{ -1,-2 },{ 2,1 },{ 2,-1 },{ -2,1 },{ -2,-1 } };struct point//建立一个结构体 存点{    int x, y, n;} p;queue<point> q;int main(){    int i, j;    cin >> n >> m;    for (i = 0; i < n; i++)        for (j = 0; j < m; j++)            map[i][j] = -1;//通过这步可以达到不用建立visit数组的效果    cin >> p.x >> p.y;    p.x--;//转为从[0,0]开始的矩阵    p.y--;    map[p.x][p.y] = 0;    q.push(p);    while (!q.empty())    {        point temp;//定义一个临时变量        for (i = 0; i < 8; i++)        {            temp = q.front();            int x = temp.x + move_[i][0];            int y = temp.y + move_[i][1];            if (x<0 || x>n || y<0 || y>m || map[x][y] != -1)                continue;//越界了就继续循环……            temp.x += move_[i][0];//cun            temp.y += move_[i][1];            temp.n++;            q.push(temp);//入队            map[temp.x][temp.y] = temp.n;        }        q.pop();    }    for (i = 0; i < n; i++)    {        for (j = 0; j < m; j++)            printf("%-5d",map[i][j]);        cout << endl;    }    return 0;}
0 0
原创粉丝点击