貪心+搜尋
貪心法就是遵從某種規律,不斷的貪心將利益最大化的方法。
解決貪心問題的方法是要找出他們之間的規律,並將利益最大化。
舉一個簡單的例子
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;
}
相關文章
- 最佳路徑搜尋(二):啟發式搜尋(代價一致搜尋(Dijkstra search),貪心搜尋,A*搜尋)
- 啟發式搜尋的方式(深度優先,廣度優先)和 搜尋方法(Dijkstra‘s演算法,代價一致搜尋,貪心搜尋 ,A星搜尋)演算法
- 貪心
- 反悔貪心
- Supermarket(貪心)
- 貪心例題
- 結合貪心搜尋和蒙特卡洛隨機遊走的面向DBpedia的實體連結方法隨機
- 讓天下沒有難用的搜尋:阿里搜尋如何成長為貼心“暖男”?阿里
- HDU 5821 Ball(貪心)
- 貪心模式記錄模式
- 反悔貪心雜題
- 貪心演算法演算法
- 貪心、構造合集
- 【GreatSQL最佳化器-04】貪婪搜尋演算法淺析SQL演算法
- eBay推出視覺搜尋工具,再也不用擔心找不到心儀物品視覺
- 貪心-刪數問題
- 貪心-*活動選擇
- 24/03/20 貪心(一)
- HDU 6047 Maximum Sequence (貪心)
- HDU 5813 Elegant Construction (貪心)Struct
- 漲薪【貪心】【快速冪】
- Leetcode 貪心:差值調整LeetCode
- Least Cost Bracket Sequence(貪心)ASTRacket
- 刪數問題(貪心)
- 「貪心」做題記錄
- [反悔貪心] Add One 2
- 貪心 做題筆記筆記
- 7.5 - 貪心篇完結
- 貪心演算法Dijkstra演算法
- 牛客 tokitsukaze and Soldier 貪心
- 海量資料搜尋---搜尋引擎
- 搜尋
- 學一下貪心演算法-學一下貪心演算法演算法
- 貪心演算法(貪婪演算法,greedy algorithm)演算法Go
- 搜尋引擎-03-搜尋引擎原理
- 牛牛偶像養成記(貪心)
- 貪心(入門簡單題)
- HDU 6299-Balanced Sequence(貪心)