# 贪心+搜索

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

#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;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

STDEN

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24
10-25 4160

11-05 9
10-29 757
02-01 38
01-21 54
07-16 161
05-03 1万+