贪心+搜索

贪心法就是遵从某种规律,不断的贪心将利益最大化的方法。
解决贪心问题的方法是要找出他们之间的规律,并将利益最大化。
举一个简单的例子
Iahub got bored, so he invented a game to be played on paper.

He writes n integers a1, a2, …, an. Each of those integers can be either 0 or 1. He’s allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values ak for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 - x.
The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are n integers: a1, a2, …, an. It is guaranteed that each of those n values is either 0 or 1.
Output
Print an integer — the maximal number of 1s that can be obtained after exactly one move.
Examples
Input
5
1 0 0 1 0
Output
4
Input
4
1 0 0 1
Output
4
题目的大意是选择一个区间【a,b】,将【a,b】之间的所有值都变成1-a[i],使得最后获得1的数量最多。

#include<stdio.h>
int main()
{
    int i,j,k,t,n,sum=0,maxx=-99,a[200],b[200];
    scanf("%d",&t);
    for( i=0;i<t;i++)
        scanf("%d",&a[i]);
    for(i=0;i<t;i++)
    {
        for(j=i;j<t;j++)
        {
            for(k=0;k<t;k++)
                b[k]=a[k];
            for(k=i;k<=j;k++)
                b[k]=1-b[k];
            sum=0;
            for(k=0;k<t;k++)
                if(b[k])
                sum++;
            if(sum>maxx)
                maxx=sum;
        }
    }
    printf("%d\n",maxx);
    return 0;
}

搜索分为深度搜索和广度搜索,深度搜索是它从某个状态开始,不断的转移状态知道无法转移,然后后退到前一步的状态,继续转移到其他状态,如此不断的反复知道找到最优的解。广度优先搜索总是先搜索距离初始状态近的状态。广度的时间复杂度要比深度搜索的小很多。
举一个广搜简单的例子
You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let’s number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let’s denote a cell on the intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (r1, c1) and this cell is cracked because you’ve just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
Input
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.
Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters “.” (that is, intact ice) and “X” (cracked ice).
The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character ‘X’ in cell (r1, c1), that is, the ice on the starting cell is initially cracked.
The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
Output
If you can reach the destination, print ‘YES’, otherwise print ‘NO’.
Examples
Input
4 6
X…XX
…XX.
.X…X.

1 6
2 2
Output
YES
Input
5 4
.X…
…X
X.X.

.XX.
5 3
1 1
Output
NO
Input
4 7
…X.XX.
.XX…X.
X…X…
X…
2 2
1 6
Output
YES
题目的大意就是你从一个点出发,每经过一个点后,就不能再通过这个点,如果目标点位“#”’你可以直接通过,如果不是“#”你需要第二次通过。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[600][600];
int n,m,p,q,ex,ey;
int to[4][2]={0,1,0,-1,1,0,-1,0};
struct ha
{
    int x,y;
}h,s;
int bfs(int x,int y)
{
    queue<ha> q;
    h.x=x;
    h.y=y;
    q.push(h);
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            h.x=s.x+to[i][0];
            h.y=s.y+to[i][1];
            if(h.x>=1&&h.x<=n&&h.y>=1&&h.y<=m)
            {
                if(h.x==ex&&h.y==ey)
                {
                    if(a[h.x][h.y]=='.')
                        a[h.x][h.y]='X';
                    else
                        return 1;
                    q.push(h);
                }
                else if(a[h.x][h.y]=='.')
                {
                    a[h.x][h.y]='X';
                    q.push(h);
                }
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
    for(int i=1;i<=n;i++)
        scanf("%s",a[i]+1);
    scanf("%d%d",&p,&q);
    scanf("%d%d",&ex,&ey);
    if(bfs(p,q))
        printf("YES\n");
    else
        printf("NO\n");
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页